1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #ifndef ZABBIX_PERSISTENT_STATE_H
21 #define ZABBIX_PERSISTENT_STATE_H
22 
23 #include "common.h"	/* for SEC_PER_DAY */
24 #include "md5.h"	/* for MD5_DIGEST_SIZE, md5_byte_t */
25 #include "zbxtypes.h"	/* for zbx_uint64_t struct st_logfile; */
26 #include "zbxalgo.h"
27 
28 #define ZBX_PERSIST_INACTIVITY_PERIOD	SEC_PER_DAY	/* the time period after which persistent files used by log */
29 							/* items which are not received in active check list can be */
30 							/* removed */
31 typedef struct
32 {
33 	char		*key_orig;
34 	char		*persistent_file_name;
35 	/* data for writing into persistent file */
36 	char		*filename;
37 	int		mtime;
38 	int		seq;
39 	int		incomplete;
40 	int		copy_of;
41 	zbx_uint64_t	dev;
42 	zbx_uint64_t	ino_lo;
43 	zbx_uint64_t	ino_hi;
44 	zbx_uint64_t	size;
45 	zbx_uint64_t	processed_size;
46 	int		md5_block_size;
47 	md5_byte_t	first_block_md5[MD5_DIGEST_SIZE];
48 	zbx_uint64_t	last_block_offset;
49 	md5_byte_t	last_block_md5[MD5_DIGEST_SIZE];
50 }
51 zbx_pre_persistent_t;
52 
53 ZBX_VECTOR_DECL(pre_persistent, zbx_pre_persistent_t)
54 
55 typedef struct
56 {
57 	char	*key_orig;
58 	time_t	not_received_time;	/* time the item was not received anymore in the list of active checks */
59 	char	*persistent_file_name;
60 }
61 zbx_persistent_inactive_t;
62 
63 ZBX_VECTOR_DECL(persistent_inactive, zbx_persistent_inactive_t)
64 
65 char	*zbx_create_persistent_server_directory(const char *base_path, const char *host, unsigned short port,
66 		char **error);
67 char	*zbx_make_persistent_file_name(const char *persistent_server_dir, const char *item_key);
68 int	zbx_read_persistent_file(const char *filename, char *buf, size_t buf_size, char **err_msg);
69 int	zbx_remove_persistent_file(const char *pathname, char **error);
70 void	zbx_write_persistent_files(zbx_vector_pre_persistent_t *prep_vec);
71 void	zbx_clean_pre_persistent_elements(zbx_vector_pre_persistent_t *prep_vec);
72 
73 void	zbx_add_to_persistent_inactive_list(zbx_vector_persistent_inactive_t *inactive_vec, char *key,
74 		const char *filename);
75 void	zbx_remove_from_persistent_inactive_list(zbx_vector_persistent_inactive_t *inactive_vec, char *key);
76 void	zbx_remove_inactive_persistent_files(zbx_vector_persistent_inactive_t *inactive_vec);
77 
78 int	zbx_find_or_create_prep_vec_element(zbx_vector_pre_persistent_t *prep_vec, const char *key,
79 		const char *persistent_file_name);
80 void	zbx_init_prep_vec_data(const struct st_logfile *logfile, zbx_pre_persistent_t *prep_vec_elem);
81 void	zbx_update_prep_vec_data(const struct st_logfile *logfile, zbx_uint64_t processed_size,
82 		zbx_pre_persistent_t *prep_vec_elem);
83 int	zbx_restore_file_details(const char *str, struct st_logfile **logfiles, int *logfiles_num,
84 		zbx_uint64_t *processed_size, int *mtime, char **err_msg);
85 #endif
86