/* callback function invoked from OpenIPMI */ static void got_control_setting(ipmi_control_t *control, int err, void *cb_data) { const char *__function_name = "got_control_setting"; zbx_ipmi_host_t *h = cb_data; zbx_ipmi_control_t *c; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (0 != err) { zabbix_log(LOG_LEVEL_DEBUG, "%s() fail: %s", __function_name, zbx_strerror(err)); h->err = zbx_dsprintf(h->err, "error 0x%x while set control", err); h->ret = NETWORK_ERROR; h->done = 1; return; } c = get_ipmi_control(h, control); if (NULL == c) { THIS_SHOULD_NEVER_HAPPEN; h->err = zbx_dsprintf(h->err, "fatal error"); h->ret = NOTSUPPORTED; h->done = 1; return; } zabbix_log(LOG_LEVEL_DEBUG, "set value completed for control %s@[%s]:%d", c->c_name, h->ip, h->port); h->done = 1; zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(h->ret)); }
static void read_ipmi_control(zbx_ipmi_host_t *h, zbx_ipmi_control_t *c) { int ret; struct timeval tv; zabbix_log(LOG_LEVEL_DEBUG, "In read_ipmi_control() %s@[%s]:%d", c->c_name, h->ip, h->port); if (0 == ipmi_control_is_readable(c->control)) { h->err = zbx_dsprintf(h->err, "Control is not readable."); h->ret = NOTSUPPORTED; return; } h->ret = SUCCEED; h->done = 0; if (0 != (ret = ipmi_control_get_val(c->control, got_control_reading, h))) { h->err = zbx_dsprintf(h->err, "Cannot read control %s." " ipmi_control_get_val() return error: 0x%x", c->c_name, ret); h->ret = NOTSUPPORTED; return; } tv.tv_sec = 10; tv.tv_usec = 0; while (0 == h->done) os_hnd->perform_one_op(os_hnd, &tv); }
static void read_ipmi_control(zbx_ipmi_host_t *h, zbx_ipmi_control_t *c) { const char *__function_name = "read_ipmi_control"; int ret; struct timeval tv; zabbix_log(LOG_LEVEL_DEBUG, "In %s() control:'%s@[%s]:%d'", __function_name, c->c_name, h->ip, h->port); if (0 == ipmi_control_is_readable(c->control)) { h->err = zbx_dsprintf(h->err, "control is not readable"); h->ret = NOTSUPPORTED; goto out; } h->ret = SUCCEED; h->done = 0; if (0 != (ret = ipmi_control_get_val(c->control, got_control_reading, h))) { h->err = zbx_dsprintf(h->err, "Cannot read control %s. ipmi_control_get_val() return error: 0x%x", c->c_name, ret); h->ret = NOTSUPPORTED; goto out; } tv.tv_sec = 10; tv.tv_usec = 0; while (0 == h->done) os_hnd->perform_one_op(os_hnd, &tv); out: zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(h->ret)); }
/****************************************************************************** * * * Function: select_discovered_host * * * * Purpose: select hostid of discovered host * * * * Parameters: dhostid - discovered host id * * * * Return value: hostid - existing hostid, 0 - if not found * * * * Author: Alexei Vladishev * * * ******************************************************************************/ static zbx_uint64_t select_discovered_host(const DB_EVENT *event) { const char *__function_name = "select_discovered_host"; DB_RESULT result; DB_ROW row; zbx_uint64_t hostid = 0; char *sql = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s() eventid:" ZBX_FS_UI64, __function_name, event->eventid); switch (event->object) { case EVENT_OBJECT_DHOST: sql = zbx_dsprintf(sql, "select h.hostid" " from hosts h,interface i,dservices ds,dchecks dc,drules dr" " where h.hostid=i.hostid" " and i.ip=ds.ip" " and ds.dcheckid=dc.dcheckid" " and dc.druleid=dr.druleid" " and " ZBX_SQL_NULLCMP("dr.proxy_hostid", "h.proxy_hostid") " and i.useip=1" " and ds.dhostid=" ZBX_FS_UI64 ZBX_SQL_NODE " order by i.hostid", event->objectid, DBand_node_local("i.interfaceid")); break; case EVENT_OBJECT_DSERVICE: sql = zbx_dsprintf(sql, "select h.hostid" " from hosts h,interface i,dservices ds,dchecks dc,drules dr" " where h.hostid=i.hostid" " and i.ip=ds.ip" " and ds.dcheckid=dc.dcheckid" " and dc.druleid=dr.druleid" " and " ZBX_SQL_NULLCMP("dr.proxy_hostid", "h.proxy_hostid") " and i.useip=1" " and ds.dserviceid =" ZBX_FS_UI64 ZBX_SQL_NODE " order by i.hostid", event->objectid, DBand_node_local("i.interfaceid")); break; default: goto exit; } result = DBselectN(sql, 1); zbx_free(sql); if (NULL != (row = DBfetch(result))) ZBX_STR2UINT64(hostid, row[0]); DBfree_result(result); exit: zabbix_log(LOG_LEVEL_DEBUG, "End of %s():" ZBX_FS_UI64, __function_name, hostid); return hostid; }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ static int process_jatrap(zbx_sock_t * sock, char *s) { int ret = SUCCEED; struct zbx_json_parse jp; char value[MAX_STRING_LEN]; char *message = NULL; static JOBARG_JOBNET_INFO ji; zbx_rtrim(s, " \r\n"); zabbix_log(LOG_LEVEL_DEBUG, "Jatrapper got [%s] len %zd", s, strlen(s)); /*JSON Format check */ if (SUCCEED != zbx_json_open(s, &jp)) { ja_log("JATRAPPER200037", 0, NULL, 0); message = zbx_dsprintf(message, "Received message error: [JSON format error]"); ret = FAIL; reply_jobresult_response(sock, ret, message); zbx_free(message); return ret; } /*[kind] check */ if (SUCCEED != zbx_json_value_by_name(&jp, JA_PROTO_TAG_KIND, value, sizeof(value))) { ja_log("JATRAPPER200036", 0, NULL, 0); message = zbx_dsprintf(message, "Received message error: [kind] not found"); ret = FAIL; reply_jobresult_response(sock, ret, message); zbx_free(message); return ret; } if (0 == strcmp(value, JA_PROTO_VALUE_JOBRESULT)) { /*from Job agent */ } else if (0 == strcmp(value, JA_PROTO_VALUE_JOBNETRUN)) { /*from external I/F */ message = evaluate_jobnetrun(sock, &jp, &ret); reply_jobnetrun_response(sock, ret, message); } else if (0 == strcmp(value, JA_PROTO_VALUE_JOBNETSTATUSRQ)) { /*from jobnet-status request */ init_jobnetinfo(&ji); message = evaluate_jobnetstatusrq(sock, &jp, &ret, &ji); reply_jobnetstatusrq_response(sock, ret, &ji, message); } else { ja_log("JATRAPPER200035", 0, NULL, 0); message = zbx_dsprintf(message, "Received message error: [kind] is not correct."); ret = FAIL; reply_jobresult_response(sock, ret, message); } zbx_free(message); return ret; }
/* callback function invoked from OpenIPMI */ static void got_control_reading(ipmi_control_t *control, int err, int *val, void *cb_data) { const char *__function_name = "got_control_reading"; zbx_ipmi_host_t *h = cb_data; int n; zbx_ipmi_control_t *c; const char *e_string; ipmi_entity_t *ent; size_t sz; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (0 != err) { zabbix_log(LOG_LEVEL_DEBUG, "%s() fail: %s", __function_name, zbx_strerror(err)); h->err = zbx_dsprintf(h->err, "error 0x%x while reading control", err); h->ret = NETWORK_ERROR; goto out; } c = get_ipmi_control(h, control); if (NULL == c) { THIS_SHOULD_NEVER_HAPPEN; h->err = zbx_dsprintf(h->err, "fatal error"); h->ret = NOTSUPPORTED; goto out; } if (c->num_values == 0) { THIS_SHOULD_NEVER_HAPPEN; h->err = zbx_dsprintf(h->err, "no value present for control"); h->ret = NOTSUPPORTED; goto out; } ent = ipmi_control_get_entity(control); e_string = ipmi_entity_get_entity_id_string(ent); for (n = 0; n < c->num_values; n++) { zabbix_log(LOG_LEVEL_DEBUG, "control values [%s | %s | %d:%d]", c->c_name, e_string, n + 1, val[n]); } sz = sizeof(int) * c->num_values; memcpy(c->val, val, sz); out: h->done = 1; zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(h->ret)); }
static void got_control_reading(ipmi_control_t *control, int err, int *val, void *cb_data) { zbx_ipmi_host_t *h = cb_data; int n; zbx_ipmi_control_t *c; const char *c_type, *e_string; ipmi_entity_t *ent; size_t sz; zabbix_log(LOG_LEVEL_DEBUG, "In got_control_reading()"); if (err) { h->err = zbx_dsprintf(h->err, "Error 0x%x while read control", err); h->ret = NETWORK_ERROR; h->done = 1; return; } c = get_ipmi_control(h, control); if (NULL == c) { /* this should never happen */ h->err = zbx_dsprintf(h->err, "Fatal error"); h->ret = NOTSUPPORTED; h->done = 1; return; } if (c->num_values == 0) { /* this should never happen */ h->err = zbx_dsprintf(h->err, "No value present for control"); h->ret = NOTSUPPORTED; h->done = 1; return; } ent = ipmi_control_get_entity(control); e_string = ipmi_entity_get_entity_id_string(ent); c_type = ipmi_control_get_type_string(control); for (n = 0; n < c->num_values; n++) { zabbix_log(LOG_LEVEL_DEBUG, "Control values [%s | %s | %d:%d]", c->c_name, e_string, n + 1, val[n]); } sz = sizeof(int) * c->num_values; memcpy(c->val, val, sz); h->done = 1; }
static int get_ifmib_general(const char *if_name, char **error) { int mib[6], ifcount; size_t len; if (NULL == if_name || '\0'== *if_name) { *error = zbx_strdup(NULL, "Network interface name cannot be empty."); return SYSINFO_RET_FAIL; } mib[0] = CTL_NET; mib[1] = PF_LINK; mib[2] = NETLINK_GENERIC; mib[3] = IFMIB_SYSTEM; mib[4] = IFMIB_IFCOUNT; len = sizeof(ifcount); if (-1 == sysctl(mib, 5, &ifcount, &len, NULL, 0)) { *error = zbx_dsprintf(NULL, "Cannot obtain number of network interfaces: %s", zbx_strerror(errno)); return SYSINFO_RET_FAIL; } mib[3] = IFMIB_IFDATA; mib[5] = IFDATA_GENERAL; len = sizeof(ifmd); for (mib[4] = 1; mib[4] <= ifcount; mib[4]++) { if (-1 == sysctl(mib, 6, &ifmd, &len, NULL, 0)) { if (ENOENT == errno) continue; *error = zbx_dsprintf(NULL, "Cannot obtain network interface information: %s", zbx_strerror(errno)); return SYSINFO_RET_FAIL; } if (0 == strcmp(ifmd.ifmd_name, if_name)) return SYSINFO_RET_OK; } *error = zbx_strdup(NULL, "Cannot find information for this network interface."); return SYSINFO_RET_FAIL; }
int VFS_FS_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { int rc, sz, i, ret = SYSINFO_RET_FAIL; struct vmount *vms = NULL, *vm; struct zbx_json j; /* check how many bytes to allocate for the mounted filesystems */ if (-1 == (rc = mntctl(MCTL_QUERY, sizeof(sz), (char *)&sz))) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } vms = zbx_malloc(vms, (size_t)sz); /* get the list of mounted filesystems */ /* return code is number of filesystems returned */ if (-1 == (rc = mntctl(MCTL_QUERY, sz, (char *)vms))) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); goto error; } zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); for (i = 0, vm = vms; i < rc; i++) { zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#FSNAME}", (char *)vm + vm->vmt_data[VMT_STUB].vmt_off, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#FSTYPE}", zbx_get_vfs_name_by_type(vm->vmt_gfstype), ZBX_JSON_TYPE_STRING); zbx_json_close(&j); /* go to the next vmount structure */ vm = (struct vmount *)((char *)vm + vm->vmt_length); } zbx_json_close(&j); SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer)); zbx_json_free(&j); ret = SYSINFO_RET_OK; error: zbx_free(vms); return ret; }
static int VM_MEMORY_AVAILABLE(AGENT_RESULT *result) { FILE *f; zbx_uint64_t value; struct sysinfo info; int res, ret = SYSINFO_RET_FAIL; /* try MemAvailable (present since Linux 3.14), falling back to a calculation based on sysinfo() and Cached */ if (NULL == (f = fopen("/proc/meminfo", "r"))) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc/meminfo: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } if (FAIL == (res = byte_value_from_proc_file(f, "MemAvailable:", "Cached:", &value))) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain the value of MemAvailable from /proc/meminfo.")); goto close; } if (SUCCEED == res) { SET_UI64_RESULT(result, value); ret = SYSINFO_RET_OK; goto close; } if (FAIL == (res = byte_value_from_proc_file(f, "Cached:", NULL, &value))) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain the value of Cached from /proc/meminfo.")); goto close; } if (NOTSUPPORTED == res) value = 0; if (0 != sysinfo(&info)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); goto close; } SET_UI64_RESULT(result, (zbx_uint64_t)(info.freeram + info.bufferram) * info.mem_unit + value); ret = SYSINFO_RET_OK; close: zbx_fclose(f); return ret; }
/****************************************************************************** * * * Function: send_script * * * * Purpose: sending command to slave node * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occurred * * * ******************************************************************************/ static int send_script(int nodeid, const char *data, char **result) { DB_RESULT db_result; DB_ROW db_row; int ret = FAIL; zbx_sock_t sock; char *answer; zabbix_log(LOG_LEVEL_DEBUG, "In send_script(nodeid:%d)", nodeid); db_result = DBselect( "select ip,port" " from nodes" " where nodeid=%d", nodeid); if (NULL != (db_row = DBfetch(db_result))) { if (SUCCEED == (ret = zbx_tcp_connect(&sock, CONFIG_SOURCE_IP, db_row[0], atoi(db_row[1]), CONFIG_TRAPPER_TIMEOUT))) { if (FAIL == (ret = zbx_tcp_send(&sock, data))) { *result = zbx_dsprintf(*result, "NODE %d: Error while sending data to Node [%d]: %s.", CONFIG_NODEID, nodeid, zbx_tcp_strerror()); goto exit_sock; } if (SUCCEED == (ret = zbx_tcp_recv(&sock, &answer))) *result = zbx_dsprintf(*result, "%s", answer); else *result = zbx_dsprintf(*result, "NODE %d: Error while receiving data from Node [%d]: %s.", CONFIG_NODEID, nodeid, zbx_tcp_strerror()); exit_sock: zbx_tcp_close(&sock); } else *result = zbx_dsprintf(*result, "NODE %d: Unable to connect to Node [%d]: %s.", CONFIG_NODEID, nodeid, zbx_tcp_strerror()); } else *result = zbx_dsprintf(*result, "NODE %d: Unknown Node ID [%d].", CONFIG_NODEID, nodeid); DBfree_result(db_result); return ret; }
int SYSTEM_UNAME(AGENT_REQUEST *request, AGENT_RESULT *result) { struct utsname name; if (-1 == uname(&name)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } SET_STR_RESULT(result, zbx_dsprintf(NULL, "%s %s %s %s %s %s", name.sysname, name.nodename, name.release, name.version, name.machine, name.idnumber)); return SYSINFO_RET_OK; }
static void add_command_alert(DC_HOST *host, zbx_uint64_t eventid, zbx_uint64_t actionid, int esc_step, const char *command, zbx_alert_status_t status, const char *error) { const char *__function_name = "add_command_alert"; zbx_uint64_t alertid; int now; char *tmp = NULL, *command_esc, *error_esc; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); alertid = DBget_maxid("alerts"); now = (int)time(NULL); tmp = zbx_dsprintf(tmp, "%s:%s", host->host, NULL == command ? "" : command); command_esc = DBdyn_escape_string_len(tmp, ALERT_MESSAGE_LEN); error_esc = DBdyn_escape_string_len(error, ALERT_ERROR_LEN); zbx_free(tmp); DBexecute("insert into alerts" " (alertid,actionid,eventid,clock,message,status,error,alerttype,esc_step)" " values " "(" ZBX_FS_UI64 "," ZBX_FS_UI64 "," ZBX_FS_UI64 ",%d,'%s',%d,'%s',%d,%d)", alertid, actionid, eventid, now, command_esc, (int)status, error_esc, ALERT_TYPE_COMMAND, esc_step); zbx_free(error_esc); zbx_free(command_esc); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
int SYSTEM_CPU_NUM(AGENT_REQUEST *request, AGENT_RESULT *result) { char *type; int name; long ncpu; if (1 < request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; } type = get_rparam(request, 0); if (NULL == type || '\0' == *type || 0 == strcmp(type, "online")) name = _SC_NPROCESSORS_ONLN; else if (0 == strcmp(type, "max")) name = _SC_NPROCESSORS_CONF; else { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; } if (-1 == (ncpu = sysconf(name))) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain number of CPUs: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } SET_UI64_RESULT(result, ncpu); return SYSINFO_RET_OK; }
int SYSTEM_CPU_INTR(AGENT_REQUEST *request, AGENT_RESULT *result) { int ret = SYSINFO_RET_FAIL; char line[MAX_STRING_LEN]; zbx_uint64_t value = 0; FILE *f; if (NULL == (f = fopen("/proc/stat", "r"))) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc/stat: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } while (NULL != fgets(line, sizeof(line), f)) { if (0 != strncmp(line, "intr", 4)) continue; if (1 != sscanf(line, "%*s " ZBX_FS_UI64, &value)) continue; SET_UI64_RESULT(result, value); ret = SYSINFO_RET_OK; break; } zbx_fclose(f); if (SYSINFO_RET_FAIL == ret) SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot find a line with \"intr\" in /proc/stat.")); return ret; }
int SYSTEM_CPU_NUM(AGENT_REQUEST *request, AGENT_RESULT *result) { #ifdef HAVE_LIBPERFSTAT char *tmp; perfstat_cpu_total_t ps_cpu_total; if (1 < request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; } tmp = get_rparam(request, 0); /* only "online" (default) for parameter "type" is supported */ if (NULL != tmp && '\0' != *tmp && 0 != strcmp(tmp, "online")) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; } if (-1 == perfstat_cpu_total(NULL, &ps_cpu_total, sizeof(ps_cpu_total), 1)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } SET_UI64_RESULT(result, ps_cpu_total.ncpus); return SYSINFO_RET_OK; #else SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for Perfstat API.")); return SYSINFO_RET_FAIL; #endif }
/****************************************************************************** * * * Function: put_data_to_server * * * * Purpose: send data to server * * * * Parameters: * * * * Return value: SUCCESS - processed successfully * * FAIL - an error occurred * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ int put_data_to_server(zbx_sock_t *sock, struct zbx_json *j, char **error) { const char *__function_name = "put_data_to_server"; char *info = NULL, *err = NULL; int ret = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In %s() datalen:" ZBX_FS_SIZE_T, __function_name, (zbx_fs_size_t)j->buffer_size); if (SUCCEED != send_data_to_server(sock, j->buffer)) goto out; if (SUCCEED != zbx_recv_response(sock, &info, 0, &err)) { *error = zbx_dsprintf(*error, "error:\"%s\", info:\"%s\"", ZBX_NULL2EMPTY_STR(err), ZBX_NULL2EMPTY_STR(info)); goto out; } ret = SUCCEED; out: zbx_free(info); zbx_free(err); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/* callback function invoked from OpenIPMI */ static void setup_done(ipmi_domain_t *domain, int err, unsigned int conn_num, unsigned int port_num, int still_connected, void *cb_data) { const char *__function_name = "setup_done"; int ret; zbx_ipmi_host_t *h = cb_data; zabbix_log(LOG_LEVEL_DEBUG, "In %s() phost:%p host:'[%s]:%d'", __function_name, h, h->ip, h->port); if (0 != err) { zabbix_log(LOG_LEVEL_DEBUG, "%s() fail: %s", __function_name, zbx_strerror(err)); h->err = zbx_dsprintf(h->err, "cannot connect to IPMI host: %s", zbx_strerror(err)); h->ret = NETWORK_ERROR; if (0 != (ret = ipmi_domain_close(domain, domain_closed, h))) zabbix_log(LOG_LEVEL_DEBUG, "cannot close IPMI domain: [0x%x]", ret); goto out; } if (0 != (ret = ipmi_domain_add_entity_update_handler(domain, entity_change, h))) zabbix_log(LOG_LEVEL_DEBUG, "ipmi_domain_add_entity_update_handler() return error: [0x%x]", ret); out: zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(h->ret)); }
int SYSTEM_UPTIME(AGENT_REQUEST *request, AGENT_RESULT *result) { #ifdef HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME int mib[2], now; size_t len; struct timeval uptime; mib[0] = CTL_KERN; mib[1] = KERN_BOOTTIME; len = sizeof(struct timeval); if (0 != sysctl(mib, 2, &uptime, &len, NULL, 0)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } now = time(NULL); SET_UI64_RESULT(result, now - uptime.tv_sec); return SYSINFO_RET_OK; #else SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for uptime information.")); return SYSINFO_RET_FAIL; #endif }
static int VM_MEMORY_CACHED(AGENT_RESULT *result) { FILE *f; zbx_uint64_t value; int res; if (NULL == (f = fopen("/proc/meminfo", "r"))) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc/meminfo: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } if (FAIL == (res = byte_value_from_proc_file(f, "Cached:", NULL, &value))) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain the value of Cached from /proc/meminfo.")); goto close; } if (NOTSUPPORTED == res) value = 0; close: zbx_fclose(f); SET_UI64_RESULT(result, value); return SYSINFO_RET_OK; }
int SYSTEM_BOOTTIME(AGENT_REQUEST *request, AGENT_RESULT *result) { #ifdef HAVE_FUNCTION_SYSCTL_KERN_BOOTTIME size_t len; int mib[2]; struct timeval boottime; mib[0] = CTL_KERN; mib[1] = KERN_BOOTTIME; len = sizeof(struct timeval); if (-1 == sysctl(mib, 2, &boottime, &len, NULL, 0)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } SET_UI64_RESULT(result, boottime.tv_sec); return SYSINFO_RET_OK; #else SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for \"kern.boottime\" system" " parameter.")); return SYSINFO_RET_FAIL; #endif }
/* get handles of eventlog */ static int zbx_get_handle_eventlog6(const wchar_t *wsource, zbx_uint64_t *lastlogsize, EVT_HANDLE *query) { const char *__function_name = "zbx_get_handle_eventlog6"; wchar_t *event_query = NULL; DWORD status = 0; char *tmp_str = NULL; int ret = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In %s(), previous lastlogsize:" ZBX_FS_UI64, __function_name, *lastlogsize); /* start building the query */ tmp_str = zbx_dsprintf(NULL, "Event/System[EventRecordID>" ZBX_FS_UI64 "]", *lastlogsize); event_query = zbx_utf8_to_unicode(tmp_str); /* create massive query for an event on a local computer*/ *query = EvtQuery(NULL, wsource, event_query, EvtQueryChannelPath); if (NULL == *query) { if (ERROR_EVT_CHANNEL_NOT_FOUND == (status = GetLastError())) zabbix_log(LOG_LEVEL_WARNING, "EvtQuery channel missed:%s", strerror_from_system(status)); else zabbix_log(LOG_LEVEL_WARNING, "EvtQuery failed:%s", strerror_from_system(status)); goto out; } ret = SUCCEED; out: zbx_free(tmp_str); zbx_free(event_query); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/* * Example: telnet.run["ls /"] */ static int telnet_run(DC_ITEM *item, AGENT_RESULT *result, const char *encoding) { const char *__function_name = "telnet_run"; zbx_sock_t s; int ret = NOTSUPPORTED, flags; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (FAIL == zbx_tcp_connect(&s, CONFIG_SOURCE_IP, item->interface.addr, item->interface.port, 0)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "cannot connect to TELNET server: %s", zbx_tcp_strerror())); goto close; } flags = fcntl(s.socket, F_GETFL); if (0 == (flags & O_NONBLOCK)) fcntl(s.socket, F_SETFL, flags | O_NONBLOCK); if (FAIL == telnet_login(s.socket, item->username, item->password, result)) goto tcp_close; if (FAIL == telnet_execute(s.socket, item->params, result, encoding)) goto tcp_close; ret = SUCCEED; tcp_close: zbx_tcp_close(&s); close: zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/* * Custom key pg.checkpoint_time_perc * * Returns the percentage of time spent writing or syncing checkpoints since * statistics were reset. * * Parameters: * 0: connection string * 1: connection database * 2: action: all (default) | write | sync * * Returns: d */ int PG_BG_TIME_PERC(AGENT_REQUEST *request, AGENT_RESULT *result) { int ret = SYSINFO_RET_FAIL; // Request result code const char *__function_name = "PG_BG_TIME_PERC"; // Function name for log file char query[MAX_STRING_LEN]; char *action = NULL, *field = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); // parse action parameter action = get_rparam(request, PARAM_FIRST); if (strisnull(action) || 0 == strcmp(action, "all")) field = "(checkpoint_write_time + checkpoint_sync_time)"; else if (0 == strcmp(action, "write")) field = "checkpoint_write_time"; else if (0 == strcmp(action, "sync")) field = "checkpoint_sync_time"; else { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Invalid action parameter: %s", action)); return ret; } // build query zbx_snprintf(query, sizeof(query), PGSQL_BG_TIME_PERC, field); // get result ret = pg_get_dbl(request, result, query, NULL); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); return ret; }
int NET_IF_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { struct if_nameindex *ni; struct zbx_json j; int i; if (NULL == (ni = if_nameindex())) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); for (i = 0; 0 != ni[i].if_index; i++) { zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#IFNAME}", ni[i].if_name, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); } if_freenameindex(ni); zbx_json_close(&j); SET_STR_RESULT(result, strdup(j.buffer)); zbx_json_free(&j); return SYSINFO_RET_OK; }
static int zbx_execute_script_on_agent(DC_HOST *host, const char *command, char **result, char *error, size_t max_error_len) { const char *__function_name = "zbx_execute_script_on_agent"; int ret; AGENT_RESULT agent_result; char *param, *port = NULL; DC_ITEM item; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); *error = '\0'; memset(&item, 0, sizeof(item)); memcpy(&item.host, host, sizeof(item.host)); if (SUCCEED != (ret = DCconfig_get_interface_by_type(&item.interface, host->hostid, INTERFACE_TYPE_AGENT))) { zbx_snprintf(error, max_error_len, "Whatap agent interface is not defined for host [%s]", host->host); goto fail; } port = zbx_strdup(port, item.interface.port_orig); substitute_simple_macros(NULL, NULL, NULL, NULL, &host->hostid, NULL, NULL, &port, MACRO_TYPE_COMMON, NULL, 0); if (SUCCEED != (ret = is_ushort(port, &item.interface.port))) { zbx_snprintf(error, max_error_len, "Invalid port number [%s]", item.interface.port_orig); goto fail; } param = zbx_dyn_escape_string(command, "\""); item.key = zbx_dsprintf(item.key, "system.run[\"%s\",\"%s\"]", param, NULL == result ? "nowait" : "wait"); item.value_type = ITEM_VALUE_TYPE_TEXT; zbx_free(param); init_result(&agent_result); alarm(CONFIG_TIMEOUT); if (SUCCEED != (ret = get_value_agent(&item, &agent_result))) { if (ISSET_MSG(&agent_result)) zbx_strlcpy(error, agent_result.msg, max_error_len); ret = FAIL; } else if (NULL != result && ISSET_TEXT(&agent_result)) *result = zbx_strdup(*result, agent_result.text); alarm(0); free_result(&agent_result); zbx_free(item.key); fail: zbx_free(port); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/********************************************************************* * zbx_module_load_config * *********************************************************************/ void zbx_module_load_config(void) { char *MODULE_CONFIG_FILE = NULL; static struct cfg_line module_cfg[] = { /* PARAMETER, VAR, TYPE, MANDATORY, MIN, MAX */ {"Param1", &CONFIG_PARAM1, TYPE_INT, PARM_OPT, 1, 100}, {"Param2", &CONFIG_PARAM2, TYPE_STRING, PARM_MAND, 0, 0}, {NULL} }; MODULE_CONFIG_FILE = zbx_dsprintf(MODULE_CONFIG_FILE, "%s/%s", CONFIG_LOAD_MODULE_PATH, MODULE_CONFIG_FILE_NAME); zabbix_log(LOG_LEVEL_DEBUG, "[%s] load conf:%s", MODULE_NAME, MODULE_CONFIG_FILE); parse_cfg_file(MODULE_CONFIG_FILE, module_cfg, ZBX_CFG_FILE_REQUIRED, ZBX_CFG_STRICT); zbx_free(MODULE_CONFIG_FILE); zbx_module_set_defaults(); }
/****************************************************************************** * * * Function: process_trap * * * * Purpose: process a single trap * * * * Parameters: addr - [IN] address of the target interface(s) * * begin - [IN] beginning of the trap message * * end - [IN] end of the trap message * * * * Author: Rudolfs Kreicbergs * * * ******************************************************************************/ static void process_trap(const char *addr, char *begin, char *end) { zbx_timespec_t ts; zbx_uint64_t *interfaceids = NULL; int count, i, ret = FAIL; char *trap = NULL; zbx_timespec(&ts); trap = zbx_dsprintf(trap, "%s%s", begin, end); count = DCconfig_get_snmp_interfaceids_by_addr(addr, &interfaceids); for (i = 0; i < count; i++) { if (SUCCEED == process_trap_for_interface(interfaceids[i], trap, &ts)) ret = SUCCEED; } if (FAIL == ret) { zbx_config_t cfg; zbx_config_get(&cfg, ZBX_CONFIG_FLAGS_SNMPTRAP_LOGGING); if (ZBX_SNMPTRAP_LOGGING_ENABLED == cfg.snmptrap_logging) zabbix_log(LOG_LEVEL_WARNING, "unmatched trap received from \"%s\": %s", addr, trap); zbx_config_clean(&cfg); } zbx_free(interfaceids); zbx_free(trap); }
int VFS_FS_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { int i, rc; struct statvfs *mntbuf; struct zbx_json j; if (0 == (rc = getmntinfo(&mntbuf, MNT_WAIT))) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); for (i = 0; i < rc; i++) { zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#FSNAME}", mntbuf[i].f_mntonname, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#FSTYPE}", mntbuf[i].f_fstypename, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); } zbx_json_close(&j); SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer)); zbx_json_free(&j); return SYSINFO_RET_OK; }
int NET_TCP_LISTEN(AGENT_REQUEST *request, AGENT_RESULT *result) { MIB_TCPTABLE *pTcpTable = NULL; DWORD dwSize, dwRetVal; int i, ret = SYSINFO_RET_FAIL; unsigned short port; char *port_str; if (1 < request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; } port_str = get_rparam(request, 0); if (NULL == port_str || SUCCEED != is_ushort(port_str, &port)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; } dwSize = sizeof(MIB_TCPTABLE); pTcpTable = (MIB_TCPTABLE *)zbx_malloc(pTcpTable, dwSize); /* Make an initial call to GetTcpTable to get the necessary size into the dwSize variable */ if (ERROR_INSUFFICIENT_BUFFER == (dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE))) pTcpTable = (MIB_TCPTABLE *)zbx_realloc(pTcpTable, dwSize); /* Make a second call to GetTcpTable to get the actual data we require */ if (NO_ERROR == (dwRetVal = GetTcpTable(pTcpTable, &dwSize, TRUE))) { for (i = 0; i < (int)pTcpTable->dwNumEntries; i++) { if (MIB_TCP_STATE_LISTEN == pTcpTable->table[i].dwState && port == ntohs((u_short)pTcpTable->table[i].dwLocalPort)) { SET_UI64_RESULT(result, 1); break; } } ret = SYSINFO_RET_OK; } else { zabbix_log(LOG_LEVEL_DEBUG, "GetTcpTable failed with error: %s", strerror_from_system(dwRetVal)); SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", strerror_from_system(dwRetVal))); goto clean; } if (!ISSET_UI64(result)) SET_UI64_RESULT(result, 0); clean: zbx_free(pTcpTable); return ret; }