zabbix_sende是zabbix_agent向zabbix_server发送数据的命令行工具,在zabbix_server端要有zabbix trapper类型的Item监控项。
只接受
指定的agent host发送来的数据。
在zabbix_sender中定义了向server端发送信息的结构体 ZBX_THREAD_SENDVAL_ARGS:
typedef struct
{
char *source_ip;
char *server;
usingned short port;
struct zbx_json json;
}
ZBX_THREAD_SENDVAL_ARGS
函数:
static int update_exit_status(int old_status, int new_status)
{
刷新退出时的状态;
}
static const char *getstring(const char*p , char* buf , size_t bufsize)
{
以‘ ’或‘\t’作为分割参数,分割参数字符串 p, 返回指向剩余字符串的指针
}
static check_response(char * response)
{
检查json数据返回状态,适用于所有active agent
int zabx_json_open(const char* buffer, struct zbx_json_parse *jp)
打开json缓存,缓存以字符串形式进行存储,将第一个{
{},}中的字段填充到zbx_json_parse结构体中。
}
struct zbx_json_parse
{
const char *start;
const char *end;
}
ZBX_THREAD_ENTRY(entry_name,arg_name)这是一个宏定义,函数原型为:
__stdcall entry_name(void *arg_name)
ZBX_THREAD_SENDVAL_ARGS *sendval_args;
typedef struct
{
char *sourceip;
char *server;
unsigned short port;
struct zbx_json json;
}
ZBX_THREAD_SENDVAL_ARGS
typedef struct
{
int server_num;
int process_num;
unsigned char process_type;
void *args;
}
zbx_thread_arg_t;
static ZBX_THREAD_ENTRY(send_value, args){
开启zbx发送数据send_value进程 ZBX_THREAD_SENDVAL_ARGS *sendval_args;
这是send_value要发送数据的结构体,由server IP、server端的端口号和agent IP组成
zbx_tcp_connect(&sock, CONFIG_SOURCE_IP, sentval_args->server, sentval_args->port, GET_SENDER_TIMEOUT)进行TCP链接
发送sendval_args中的 json->buffer
关闭TCP
查看发送结果
记录日志
zbx_thread_exit()进程退出
}
解析配置文件需要用到的结构体,命令行结构体struct cfg_line
struct cfg_line
{
char *parameter; //配置文件中的参数
void *variable; //配置文件参数变量的值(有些不确定!!!)
int type; //变量的类型;
int mandatory ; // 强制性;
zbx_unit64_t min; //参数最小值
zbc_unit64_t max ;//参数最大值/
}
static void zbx_load_config(const char* config_file)
{//加载配置文件
char* cfg_source_ip ,//指定源IP
cfg_active_hosts, //主动状态的主机
cfg_hostname;//主机名
struct cfg_line cfg[] = {
{“SourceIP”, &cfg_source_ip, TYPE_STRING,
PARM_OPT,0,0},
{“ServerActive”, &cfg_active_hosts, TYPE_STRING_LIST,
PARAM_OPT,0,0 },
{“Hostname”, &cfg_hostname,
TYPE_STRING,
PARM_OPT,0,0},
{NULL}
}
parse_cfg_file(config_file, cfg, ZBX_CFG_FILE_REQUIRED,ZBX_CFG_NOT_STRICT)
这个函数对日志进行解析,填充结构体当中空缺的variable 变量和参数的最大值和最小值,ZBX_CFG_FILE_REQUIRED宏定义为0,ZBX_CFG_NOT_STRICT宏定义为1,
刷新CONFIG_SOURCE_IP;
cfg_active_hosts传值给ZABBIX_SERVER,cfg_hostname传值给ZABBIX_HOSTNAME,cfg_server_port传值给ZABBIX_SERVER_PORT,cfg_source_ip传值给CONFIG_SOURCE_IP;
释放cfg_active_hosts, cfg_hostname,cfg_server_port, cfg_source_ip占用的内存;
}
使用到的结构体:
struct zbx_option
{
const char* name;
int has_arg;
int *flag;
int val;
}
static parse_commandline(int argc, char** argv)
{
zbx_getopt_long(int atgc, cahr** argv, const char* option , const struct zbx_option * long_option, int* opt_index)
解析命令行进行解析,option是以‘-’开头的字符串,long_option是zbx_option类型的vector,opt_index
}