diff options
| author | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2007-03-29 14:07:35 +0000 |
|---|---|---|
| committer | osmiy <osmiy@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2007-03-29 14:07:35 +0000 |
| commit | 5ca707a4565f6abfa0a29981247a94f4b84c33bc (patch) | |
| tree | e97106cf177ae42efce50d3a0d1dc9f4bb9503c6 /src/zabbix_sender | |
| parent | 819c886a68348761914d55a23f8bff429caadc4f (diff) | |
| download | zabbix-5ca707a4565f6abfa0a29981247a94f4b84c33bc.tar.gz zabbix-5ca707a4565f6abfa0a29981247a94f4b84c33bc.tar.xz zabbix-5ca707a4565f6abfa0a29981247a94f4b84c33bc.zip | |
- improved ZABBIX sender (Eugene)
- fixed popup menu on frontend for hosts screen (Eugene)
- monir fixes of C code (Eugene)
git-svn-id: svn://svn.zabbix.com/trunk@3953 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src/zabbix_sender')
| -rw-r--r-- | src/zabbix_sender/zabbix_sender.c | 489 |
1 files changed, 364 insertions, 125 deletions
diff --git a/src/zabbix_sender/zabbix_sender.c b/src/zabbix_sender/zabbix_sender.c index 7668cf3a..62c68a88 100644 --- a/src/zabbix_sender/zabbix_sender.c +++ b/src/zabbix_sender/zabbix_sender.c @@ -18,62 +18,106 @@ **/ #include "common.h" + +#include "threads.h" #include "comms.h" +#include "cfg.h" +#include "log.h" #include "zbxgetopt.h" char *progname = NULL; + char title_message[] = "ZABBIX send"; -char usage_message[] = "[-vh] {-zpsko | -i}"; + +char usage_message[] = "[-Vhv] {[-zps] -ko | -i <file>} [-c <file>]"; #ifdef HAVE_GETOPT_LONG char *help_message[] = { "Options:", - " -z --zabbix-server <zabbix_server> Hostname or IP address of ZABBIX Server.", - " -p --port <zabbix_server_port> Specify port number of server trapper running on the server. Default is 10051.", - " -s --host <hostname> Specify hostname or IP address of a host.", - " -k --key <key_of_metric> Specify metric name (key) we want to send.", - " -o --value <value> Specify value of the key.", - " -i --input-file <input_file> Load values from input file.", - " Each line of file contains: <zabbix_server> <hostname> <port> <key> <value>.", - " -h --help Give this help.", - " -v --version Display version number.", + " -c --config <File> Specify configuration file" + "", + " -z --zabbix-server <Server> Hostname or IP address of ZABBIX Server", + " -p --port <Server port> Specify port number of server trapper running on the server. Default is 10051", + " -s --host <Hostname> Specify hostname or IP address of a host", + "", + " -k --key <Key> Specify metric name (key) we want to send", + " -o --value <Key value> Specify value of the key", + "", + " -i --input-file <input_file> Load values from input file", + " Each line of file contains: <zabbix_server> <hostname> <port> <key> <value>", + "", + " -v --verbose Verbose mode", + "", + " Other options:", + " -h --help Give this help", + " -V --version Display version number", 0 /* end of text */ }; #else char *help_message[] = { "Options:", - " -z <zabbix_server> Hostname or IP address of ZABBIX Server.", - " -p <zabbix_server_port> Specify port number of server trapper running on the server. Default is 10051.", - " -s <hostname> Specify hostname or IP address of a host.", - " -k <key_of_metric> Specify metric name (key) we want to send.", - " -o <value> Specify value of the key.", - " -i <input_file> Load values from input file.", - " Each line of file contains: <zabbix_server> <hostname> <port> <key> <value>.", - " -h Give this help.", - " -v Display version number.", + " -c <File> Specify configuration file" + "", + " -z <Server> Hostname or IP address of ZABBIX Server.", + " -p <Server port> Specify port number of server trapper running on the server. Default is 10051.", + " -s <Hostname> Specify hostname or IP address of a host.", + "", + " -k <Key> Specify metric name (key) we want to send.", + " -o <Key value> Specify value of the key.", + "", + " -i <Input file> Load values from input file.", + " Each line of file contains: <zabbix_server> <hostname> <port> <key> <value>.", + "", + " -v Verbose mode", + "", + " Other options:", + " -h Give this help.", + " -V Display version number.", 0 /* end of text */ }; #endif -struct zbx_option longopts[] = +/* COMMAND LINE OPTIONS */ + +/* long options */ + +static struct zbx_option longopts[] = { - {"zabbix-server", 1, NULL, 'z'}, - {"port", 1, NULL, 'p'}, - {"host", 1, NULL, 's'}, - {"key", 1, NULL, 'k'}, - {"value", 1, NULL, 'o'}, - {"input-file", 1, NULL, 'i'}, - {"help", 0, NULL, 'h'}, - {"version", 0, NULL, 'v'}, - {0,0,0,0} + {"config", 1, NULL, 'c'}, + {"zabbix-server", 1, NULL, 'z'}, + {"port", 1, NULL, 'p'}, + {"host", 1, NULL, 's'}, + {"key", 1, NULL, 'k'}, + {"value", 1, NULL, 'o'}, + {"input-file", 1, NULL, 'i'}, + {"verbose", 0, NULL, 'v'}, + {"help", 0, NULL, 'h'}, + {"version", 0, NULL, 'V'}, + {0,0,0,0} }; -static void zbx_sender_signal_handler( int sig ) +/* short options */ + +static char shortopts[] = "c:z:p:s:k:o:i:vhV"; + +/* end of COMMAND LINE OPTIONS*/ + +static int CONFIG_LOG_LEVEL = LOG_LEVEL_CRIT; + +static char* INPUT_FILE = NULL; + +static char* ZABBIX_SERVER = NULL; +int ZABBIX_SERVER_PORT = 0; +static char* ZABBIX_HOSTNAME = NULL; +static char* ZABBIX_KEY = NULL; +static char* ZABBIX_KEY_VALUE = NULL; + +static void send_signal_handler( int sig ) { if( SIGALRM == sig ) { - signal( SIGALRM, zbx_sender_signal_handler ); - fprintf(stderr,"Timeout while executing operation.\n"); + signal( SIGALRM, send_signal_handler ); + zabbix_log( LOG_LEVEL_WARNING, "Timeout while executing operation"); } if( SIGQUIT == sig || SIGINT == sig || SIGTERM == sig ) @@ -83,136 +127,331 @@ static void zbx_sender_signal_handler( int sig ) exit( FAIL ); } -static int send_value(char *server,int port,char *hostname, char *key,char *value, char *lastlogsize) +typedef struct zbx_active_metric_type { + char* server; + int port; + char* hostname; + char* key; + char* key_value; +} ZBX_THREAD_SENDVAL_ARGS; + +static ZBX_THREAD_ENTRY(send_value, args) +{ + ZBX_THREAD_SENDVAL_ARGS *sentdval_args; + char tosend[MAX_STRING_LEN]; - char foo[MAX_STRING_LEN]; - zbx_sock_t sock; - char *answer; - if( FAIL == zbx_tcp_connect(&sock, server, port)) - { - return FAIL; - } + char *answer = NULL; - foo[0] = '\0'; - comms_create_request(hostname, key, value, lastlogsize, foo, foo, foo, tosend, sizeof(tosend)-1); - if( FAIL == zbx_tcp_send(&sock, tosend)) - { - zbx_tcp_close(&sock); - return FAIL; - } + int ret = FAIL; - if( FAIL == zbx_tcp_recv(&sock, &answer)) - { - zbx_tcp_close(&sock); - return FAIL; - } + assert(args); + + sentdval_args = ((ZBX_THREAD_SENDVAL_ARGS *)args); + + + zabbix_log( LOG_LEVEL_DEBUG, "Send to: '%s:%i' As: '%s' Key: '%s' Value: '%s'", + sentdval_args->server, + sentdval_args->port, + sentdval_args->hostname, + sentdval_args->key, + sentdval_args->key_value + ); + + signal( SIGINT, send_signal_handler ); + signal( SIGQUIT, send_signal_handler ); + signal( SIGTERM, send_signal_handler ); + signal( SIGALRM, send_signal_handler ); - if(strcmp(answer,"OK") == 0) + alarm(SENDER_TIMEOUT); + + if( FAIL != zbx_tcp_connect(&sock, sentdval_args->server, sentdval_args->port) ) { - printf("OK\n"); + comms_create_request(sentdval_args->hostname, sentdval_args->key, sentdval_args->key_value, + NULL, NULL, NULL, NULL, tosend, sizeof(tosend)-1); + + zabbix_log( LOG_LEVEL_DEBUG, "Send data: '%s'", tosend); + + if( FAIL != zbx_tcp_send(&sock, tosend) ) + { + if( FAIL != zbx_tcp_recv(&sock, &answer) ) + { + if( !answer || strcmp(answer,"OK") ) + { + zabbix_log( LOG_LEVEL_WARNING, "Incorrect answer from server [%s]", answer); + } + else + { + ret = SUCCEED; + } + } + } + zbx_tcp_close(&sock); } - zbx_tcp_close(&sock); + alarm(0); - return SUCCEED; + zbx_tread_exit(ret); } -int main(int argc, char **argv) +static void init_config(const char* config_file) { - int ret=SUCCEED; - char line[MAX_STRING_LEN]; - char port_str[MAX_STRING_LEN]="10051"; /* default value */ - char zabbix_server[MAX_STRING_LEN]=""; - char server[MAX_STRING_LEN]=""; - char key[MAX_STRING_LEN]=""; - char value[MAX_STRING_LEN]=""; - char input_file[MAX_STRING_LEN]=""; - int port_specified = 0; - char *s; - int ch; - FILE *in; - - progname = argv[0]; - - signal( SIGINT, zbx_sender_signal_handler ); - signal( SIGQUIT, zbx_sender_signal_handler ); - signal( SIGTERM, zbx_sender_signal_handler ); - signal( SIGALRM, zbx_sender_signal_handler ); - - while ((ch = zbx_getopt_long(argc, argv, "z:p:s:k:o:i:hv", longopts, NULL)) != EOF) + char* zabbix_server_from_conf = NULL; + int zabbix_server_port_from_conf = 0; + char* zabbix_hostname_from_conf = NULL; + char* c = NULL; + + struct cfg_line cfg[]= + { + /* PARAMETER ,VAR ,FUNC ,TYPE(0i,1s) ,MANDATORY ,MIN ,MAX */ + {"Server" ,&zabbix_server_from_conf ,0 ,TYPE_STRING ,PARM_OPT ,0 ,0 }, + {"ServerPort" ,&zabbix_server_port_from_conf ,0 ,TYPE_INT ,PARM_OPT ,MIN_ZABBIX_PORT ,MAX_ZABBIX_PORT}, + {"Hostname" ,&zabbix_hostname_from_conf ,0 ,TYPE_STRING ,PARM_OPT ,0 ,0 }, + {0} + }; + + if( config_file ) { - switch (ch) + parse_cfg_file(config_file, cfg); + + if( zabbix_server_from_conf ) { - case 'z': if (zbx_optarg) strcpy(zabbix_server, zbx_optarg); break; - case 'p': if (zbx_optarg) strcpy(port_str, zbx_optarg); port_specified=1; break; - case 's': if (zbx_optarg) strcpy(server, zbx_optarg); break; - case 'k': if (zbx_optarg) strcpy(key, zbx_optarg); break; - case 'o': if (zbx_optarg) strcpy(value, zbx_optarg); break; - case 'i': if (zbx_optarg) strcpy(input_file, zbx_optarg); break; - case 'v': version(); return 0; - case 'h': default: help(); return 0; + if( !ZABBIX_SERVER ) + { /* apply parameter only if unsetted */ + if( (c = strchr(zabbix_server_from_conf, ',')) ) + { /* get only first server */ + *c = '\0'; + } + ZABBIX_SERVER = strdup(zabbix_server_from_conf); + } + zbx_free(zabbix_server_from_conf); } - } + if( !ZABBIX_SERVER_PORT && zabbix_server_port_from_conf ) + { /* apply parameter only if unsetted */ + ZABBIX_SERVER_PORT = zabbix_server_port_from_conf; + } - if (zabbix_server[0] && server[0] && key[0] && value[0]) - { - if (input_file[0]) + if( zabbix_hostname_from_conf ) { - help(); return FAIL; + if( !ZABBIX_HOSTNAME ) + { /* apply parameter only if unsetted */ + ZABBIX_HOSTNAME = strdup(zabbix_hostname_from_conf); + } + zbx_free(zabbix_hostname_from_conf); } - - alarm(SENDER_TIMEOUT); + } +} - /*printf("Run with options: z=%s, p=%s, s=%s, k=%s, o=%s\n", zabbix_server, port_str, server, key, value);*/ - ret = send_value(zabbix_server, atoi(port_str), server, key, value,"0"); +static zbx_task_t parse_commandline(int argc, char **argv) +{ + zbx_task_t task = ZBX_TASK_START; + char ch = '\0'; - alarm(0); - } -/* No parameters are given */ - else if (input_file[0]) - { - if (zabbix_server[0] || server[0] || key[0] || value[0] || port_specified) { - help(); return FAIL; + /* Parse the command-line. */ + while ((ch = zbx_getopt_long(argc, argv, shortopts, longopts, NULL)) != EOF) + switch ((char) ch) { + case 'c': + CONFIG_FILE = strdup(zbx_optarg); + break; + case 'h': + help(); + exit(-1); + break; + case 'V': + version(); + exit(-1); + break; + case 'z': + ZABBIX_SERVER = strdup(zbx_optarg); + break; + case 'p': + ZABBIX_SERVER_PORT = atoi(zbx_optarg); + break; + case 's': + ZABBIX_HOSTNAME = strdup(zbx_optarg); + break; + case 'k': + ZABBIX_KEY = strdup(zbx_optarg); + break; + case 'o': + ZABBIX_KEY_VALUE = strdup(zbx_optarg); + break; + case 'i': + INPUT_FILE = strdup(zbx_optarg); + break; + case 'v': + if(CONFIG_LOG_LEVEL == LOG_LEVEL_WARNING) + CONFIG_LOG_LEVEL = LOG_LEVEL_DEBUG; + else + CONFIG_LOG_LEVEL = LOG_LEVEL_WARNING; + break; + default: + usage(); + exit(FAIL); + break; } - in = fopen(input_file, "r"); - if (!in) + return task; +} + +int main(int argc, char **argv) +{ + FILE *in; + + char in_line[MAX_STRING_LEN], + *str_port, + *s; + + int task = ZBX_TASK_START, + total_count = 0, + succeed_count = 0, + ret = SUCCEED; + + ZBX_THREAD_SENDVAL_ARGS sentdval_args; + + progname = get_programm_name(argv[0]); + + task = parse_commandline(argc, argv); + + init_config(CONFIG_FILE); + + zabbix_open_log(LOG_TYPE_UNDEFINED, CONFIG_LOG_LEVEL, NULL); + + if( INPUT_FILE ) + { + if( !(in = fopen(INPUT_FILE, "r")) ) { - fprintf(stderr, "%s: no such file.\n", input_file); + zabbix_log( LOG_LEVEL_WARNING, "Cannot open [%s] [%s]", INPUT_FILE, strerror(errno)); return FAIL; } - while(fgets(line, MAX_STRING_LEN, in) != NULL) - { - alarm(SENDER_TIMEOUT); + while(fgets(in_line, sizeof(in_line), in) != NULL) + { /* <zabbix_server> <hostname> <port> <key> <value> */ + total_count++; /* also used as inputline */ - s=(char *)strtok(line," "); - strscpy(zabbix_server,s); - s=(char *)strtok(NULL," "); - strscpy(server,s); - s=(char *)strtok(NULL," "); - strscpy(port_str,s); - s=(char *)strtok(NULL," "); - strscpy(key,s); - s=(char *)strtok(NULL," "); - strscpy(value,s); - /*printf("Run with options from file(%s): z=%s, s=%s, p=%s, k=%s, o=%s\n", input_file, zabbix_server, server, port_str, key, value);*/ - ret = send_value(zabbix_server,atoi(port_str),server,key,value,"0"); - - alarm(0); + sentdval_args.server = in_line; + + if( !(sentdval_args.hostname = strchr(sentdval_args.server, ' ')) ) + { + zabbix_log( LOG_LEVEL_WARNING, "[line %i] 'Server' required", total_count); + continue; + } + + *sentdval_args.hostname = '\0'; + sentdval_args.hostname++; + + if( !(str_port = strchr(sentdval_args.hostname, ' ')) ) + { + zabbix_log( LOG_LEVEL_WARNING, "[line %i] 'Server port' required", total_count); + continue; + } + + *str_port = '\0'; + str_port++; + + if( !(sentdval_args.key = strchr(str_port, ' ')) ) + { + zabbix_log( LOG_LEVEL_WARNING, "[line %i] 'Key' required", total_count); + continue; + } + + *sentdval_args.key = '\0'; + sentdval_args.key++; + + if( !(sentdval_args.key_value = strchr(sentdval_args.key, ' ')) ) + { + zabbix_log( LOG_LEVEL_WARNING, "[line %i] 'Key value' required", total_count); + continue; + } + + *sentdval_args.key_value = '\0'; + sentdval_args.key_value++; + + for(s = sentdval_args.key_value; s && *s; s++) + { + if(*s == '\r' || *s == '\n' ) + { + *s = '\0'; + break; + } + } + + if( ZABBIX_SERVER ) sentdval_args.server = ZABBIX_SERVER; + if( ZABBIX_HOSTNAME ) sentdval_args.hostname = ZABBIX_HOSTNAME; + if( ZABBIX_KEY ) sentdval_args.key = ZABBIX_KEY; + if( ZABBIX_KEY_VALUE ) sentdval_args.key_value = ZABBIX_KEY_VALUE; + + if( ZABBIX_SERVER_PORT ) + sentdval_args.port = ZABBIX_SERVER_PORT; + else + sentdval_args.port = atoi(str_port); + + if( MIN_ZABBIX_PORT > sentdval_args.port || sentdval_args.port > MAX_ZABBIX_PORT ) + { + zabbix_log( LOG_LEVEL_WARNING, "[line %i] Incorrect port number [%i]. Allowed [%i:%i]", + total_count, sentdval_args.port, MIN_ZABBIX_PORT, MAX_ZABBIX_PORT); + continue; + } + + if( SUCCEED == zbx_thread_wait( + zbx_thread_start( + send_value, + &sentdval_args + ) + ) + ) + succeed_count++; + } + + fclose(in); } else { - help(); - ret = FAIL; + total_count++; + + do /* try block simulation */ + { + if( !ZABBIX_SERVER ) { zabbix_log( LOG_LEVEL_WARNING, "'Server' parameter required"); break; } + if( !ZABBIX_HOSTNAME ) { zabbix_log( LOG_LEVEL_WARNING, "'Hostname' parameter required"); break; } + if( !ZABBIX_KEY ) { zabbix_log( LOG_LEVEL_WARNING, "Key required"); break; } + if( !ZABBIX_KEY_VALUE ) { zabbix_log( LOG_LEVEL_WARNING, "Key value required"); break; } + + if( !ZABBIX_SERVER_PORT ) ZABBIX_SERVER_PORT = 10051; + + if( MIN_ZABBIX_PORT > ZABBIX_SERVER_PORT || ZABBIX_SERVER_PORT > MAX_ZABBIX_PORT ) + { + zabbix_log( LOG_LEVEL_WARNING, "Incorrect port number [%i]. Allowed [%i:%i]", + ZABBIX_SERVER_PORT, MIN_ZABBIX_PORT, MAX_ZABBIX_PORT); + break; + } + + sentdval_args.server = ZABBIX_SERVER; + sentdval_args.port = ZABBIX_SERVER_PORT; + sentdval_args.hostname = ZABBIX_HOSTNAME; + sentdval_args.key = ZABBIX_KEY; + sentdval_args.key_value = ZABBIX_KEY_VALUE; + + if( SUCCEED == zbx_thread_wait( + zbx_thread_start( + send_value, + &sentdval_args + ) + ) + ) + succeed_count++; + } + while(0); /* try block simulation */ } + printf("sent: %i; failed: %i; total: %i\n", succeed_count, (total_count - succeed_count), total_count); + + zabbix_close_log(); + return ret; } |
