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
}