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; }
int NET_IF_DISCOVERY(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { struct if_nameindex *ni; struct zbx_json j; int i; zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); for (ni = if_nameindex(), 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; }
/* * Custom key pg.table.discovery * * Returns all known Tables in a PostgreSQL database * * Parameter [0-4]: <host,port,db,user,passwd> * * * Returns: * { * "data":[ * { * "{#DATABASE}":"MyDatabase", * "{#SCHEMA}":"public", * "{#TABLESPACE}":"pg_default", * "{#TABLE}":"MyTable", * "{#TYPE}":"MyTable", * "{#OWNER}":"postgres", * "{#PERSISTENCE":"permenant|temporary", * "{#ISSUBCLASS}":"0"}]} */ int PG_TABLE_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { int ret = SYSINFO_RET_FAIL; // Request result code const char *__function_name = "PG_TABLE_DISCOVERY"; // Function name for log file struct zbx_json j; // JSON response for discovery rule PGconn *conn = NULL; PGresult *res = NULL; char query[MAX_STRING_LEN] = PGSQL_DISCOVER_TABLES; int i = 0, count = 0; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); // Connect to PostreSQL if(NULL == (conn = pg_connect(request))) goto out; // Execute a query res = PQexec(conn, query); if(PQresultStatus(res) != PGRES_TUPLES_OK) { zabbix_log(LOG_LEVEL_ERR, "Failed to execute PostgreSQL query in %s() with: %s", __function_name, PQresultErrorMessage(res)); goto out; } if(0 == (count = PQntuples(res))) { zabbix_log(LOG_LEVEL_DEBUG, "No results returned for query \"%s\" in %s()", query, __function_name); } // Create JSON array of discovered objects zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); for(i = 0; i < count; i++) { zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#OID}", PQgetvalue(res, i, 0), ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#DATABASE}", PQgetvalue(res, i, 1), ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#SCHEMA}", PQgetvalue(res, i, 2), ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#TABLESPACE}", PQgetvalue(res, i, 3), ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#TABLE}", PQgetvalue(res, i, 4), ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#TYPE}", PQgetvalue(res, i, 5), ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#OWNER}", PQgetvalue(res, i, 6), ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#PERSISTENCE}", PQgetvalue(res, i, 7), ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#ISSUBCLASS}", PQgetvalue(res, i, 8), ZBX_JSON_TYPE_STRING); zbx_json_close(&j); } // Finalize JSON response zbx_json_close(&j); SET_STR_RESULT(result, strdup(j.buffer)); zbx_json_free(&j); ret = SYSINFO_RET_OK; out: PQclear(res); PQfinish(conn); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); return ret; }
int NET_IF_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { int ret = SYSINFO_RET_FAIL, i; struct zbx_json j; struct if_nameindex *interfaces; zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); if (NULL != (interfaces = if_nameindex())) { i = 0; while (0 != interfaces[i].if_index) { zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#IFNAME}", interfaces[i].if_name, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); i++; } ret = SYSINFO_RET_OK; } zbx_json_close(&j); SET_STR_RESULT(result, strdup(j.buffer)); zbx_json_free(&j); return ret; }
int VFS_FS_DISCOVERY(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { int i, rc, ret = SYSINFO_RET_FAIL; struct statfs *mntbuf; struct zbx_json j; zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); if (0 != (rc = getmntinfo(&mntbuf, MNT_WAIT))) { 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); } ret = SYSINFO_RET_OK; } zbx_json_close(&j); SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer)); zbx_json_free(&j); return ret; }
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_IF_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { #if defined(HAVE_LIBPERFSTAT) int rc, i, ret = SYSINFO_RET_FAIL; perfstat_id_t ps_id; perfstat_netinterface_t *ps_netif = NULL; struct zbx_json j; /* check how many perfstat_netinterface_t structures are available */ if (-1 == (rc = perfstat_netinterface(NULL, NULL, sizeof(perfstat_netinterface_t), 0))) { 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); if (0 == rc) /* no network interfaces found */ { ret = SYSINFO_RET_OK; goto end; } ps_netif = zbx_malloc(ps_netif, rc * sizeof(perfstat_netinterface_t)); /* set name to first interface */ strscpy(ps_id.name, FIRST_NETINTERFACE); /* pseudo-name for the first network interface */ /* ask to get all the structures available in one call */ /* return code is number of structures returned */ if (-1 != (rc = perfstat_netinterface(&ps_id, ps_netif, sizeof(perfstat_netinterface_t), rc))) ret = SYSINFO_RET_OK; /* collecting of the information for each of the interfaces */ for (i = 0; i < rc; i++) { zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#IFNAME}", ps_netif[i].name, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); } zbx_free(ps_netif); end: zbx_json_close(&j); SET_STR_RESULT(result, strdup(j.buffer)); zbx_json_free(&j); return ret; #else SET_MSG_RESULT(result, zbx_strdup(NULL, "Agent was compiled without support for Perfstat API.")); return SYSINFO_RET_FAIL; #endif }
int check_vcenter_vm_vfs_fs_discovery(AGENT_REQUEST *request, const char *username, const char *password, AGENT_RESULT *result) { struct zbx_json json_data; zbx_vmware_service_t *service; zbx_vmware_vm_t *vm = NULL; char *url, *uuid; zbx_vector_str_t disks; int i, ret = SYSINFO_RET_FAIL; if (2 != request->nparam) return SYSINFO_RET_FAIL; url = get_rparam(request, 0); uuid = get_rparam(request, 1); if ('\0' == *uuid) return SYSINFO_RET_FAIL; zbx_vmware_lock(); if (NULL == (service = get_vmware_service(url, username, password, result, &ret))) goto unlock; if (NULL == (vm = service_vm_get(service, uuid))) goto unlock; zbx_vector_str_create(&disks); zbx_xml_read_values(vm->details, ZBX_XPATH_LN2("disk", "diskPath"), &disks); zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA); for (i = 0; i < disks.values_num; i++) { zbx_json_addobject(&json_data, NULL); zbx_json_addstring(&json_data, "{#FSNAME}", disks.values[i], ZBX_JSON_TYPE_STRING); zbx_json_close(&json_data); } zbx_json_close(&json_data); zbx_vector_str_clean(&disks); zbx_vector_str_destroy(&disks); SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer)); zbx_json_free(&json_data); ret = SYSINFO_RET_OK; unlock: zbx_vmware_unlock(); return ret; }
int check_vcenter_hv_discovery(AGENT_REQUEST *request, const char *username, const char *password, AGENT_RESULT *result) { struct zbx_json json_data; int i, ret = SYSINFO_RET_FAIL; char *url, *name; zbx_vmware_service_t *service; if (1 != request->nparam) return SYSINFO_RET_FAIL; url = get_rparam(request, 0); zbx_vmware_lock(); if (NULL == (service = get_vmware_service(url, username, password, result, &ret))) goto unlock; zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA); for (i = 0; i < service->data->hvs.values_num; i++) { zbx_vmware_cluster_t *cluster = NULL; zbx_vmware_hv_t *hv = (zbx_vmware_hv_t *)service->data->hvs.values[i]; if (NULL == (name = zbx_xml_read_value(hv->details, ZBX_XPATH_LN2("config", "name")))) continue; if (NULL != hv->clusterid) cluster = cluster_get(&service->data->clusters, hv->clusterid); zbx_json_addobject(&json_data, NULL); zbx_json_addstring(&json_data, "{#HV.UUID}", hv->uuid, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json_data, "{#HV.ID}", hv->id, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json_data, "{#HV.NAME}", name, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json_data, "{#CLUSTER.NAME}", (NULL != cluster ? cluster->name : ""), ZBX_JSON_TYPE_STRING); zbx_json_close(&json_data); zbx_free(name); } zbx_json_close(&json_data); SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer)); zbx_json_free(&json_data); ret = SYSINFO_RET_OK; unlock: zbx_vmware_unlock(); return ret; }
int check_vcenter_vm_vfs_dev_discovery(AGENT_REQUEST *request, const char *username, const char *password, AGENT_RESULT *result) { struct zbx_json json_data; zbx_vmware_service_t *service; zbx_vmware_vm_t *vm = NULL; zbx_vmware_dev_t *dev; char *url, *uuid; int i, ret = SYSINFO_RET_FAIL; if (2 != request->nparam) return SYSINFO_RET_FAIL; url = get_rparam(request, 0); uuid = get_rparam(request, 1); if ('\0' == *uuid) return SYSINFO_RET_FAIL; zbx_vmware_lock(); if (NULL == (service = get_vmware_service(url, username, password, result, &ret))) goto unlock; if (NULL == (vm = service_vm_get(service, uuid))) goto unlock; zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA); for (i = 0; i < vm->devs.values_num; i++) { dev = (zbx_vmware_dev_t *)vm->devs.values[i]; if (ZBX_VMWARE_DEV_TYPE_DISK != dev->type) continue; zbx_json_addobject(&json_data, NULL); zbx_json_addstring(&json_data, "{#DISKNAME}", dev->instance, ZBX_JSON_TYPE_STRING); zbx_json_close(&json_data); } zbx_json_close(&json_data); SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer)); zbx_json_free(&json_data); ret = SYSINFO_RET_OK; unlock: zbx_vmware_unlock(); return ret; }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ void reply_jobnetstatusrq_response(zbx_sock_t * sock, int ret, JOBARG_JOBNET_INFO * ji, char *message) { struct zbx_json json; /*JSON Data */ zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&json, JA_PROTO_TAG_KIND, JA_PROTO_VALUE_JOBNETSTATUSRQ_RES, ZBX_JSON_TYPE_STRING); zbx_json_adduint64(&json, JA_PROTO_TAG_VERSION, JA_PROTO_VALUE_VERSION_1); /*data version */ zbx_json_addstring(&json, JA_PROTO_TAG_SERVERID, serverid, ZBX_JSON_TYPE_STRING); zbx_json_addobject(&json, JA_PROTO_TAG_DATA); zbx_json_adduint64(&json, JA_PROTO_TAG_RESULT, FAIL == ret ? 1 : 0); if (ret != FAIL) { zbx_json_addstring(&json, JA_PROTO_TAG_JOBNETID, ji->jobnetid, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, JA_PROTO_TAG_JOBNETNAME, ji->jobnetname, ZBX_JSON_TYPE_STRING); zbx_json_adduint64(&json, JA_PROTO_TAG_SCHEDULEDTIME, ji->scheduled_time); zbx_json_adduint64(&json, JA_PROTO_TAG_STARTTIME, ji->start_time); zbx_json_adduint64(&json, JA_PROTO_TAG_ENDTIME, ji->end_time); zbx_json_adduint64(&json, JA_PROTO_TAG_JOBNETRUNTYPE, ji->jobnetruntype); zbx_json_adduint64(&json, JA_PROTO_TAG_JOBNETSTATUS, ji->jobnetstatus); zbx_json_adduint64(&json, JA_PROTO_TAG_JOBSTATUS, ji->jobstatus); zbx_json_addstring(&json, JA_PROTO_TAG_LASTEXITCD, ji->lastexitcd, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, JA_PROTO_TAG_LASTSTDOUT, ji->laststdout, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, JA_PROTO_TAG_LASTSTDERR, ji->laststderr, ZBX_JSON_TYPE_STRING); } else { zbx_json_addstring(&json, JA_PROTO_TAG_MESSAGE, message, ZBX_JSON_TYPE_STRING); } zbx_json_close(&json); zbx_json_close(&json); zabbix_log(LOG_LEVEL_DEBUG, "JSON before sending [%s]", json.buffer); if (FAIL == (ret = zbx_tcp_send_to(sock, json.buffer, CONFIG_TIMEOUT))) { zbx_free(ji->jobnetid); zbx_free(ji->jobnetname); ja_log("JATRAPPER200038", 0, NULL, 0, zbx_tcp_strerror()); ret = NETWORK_ERROR; } zbx_free(ji->jobnetid); zbx_free(ji->jobnetname); zbx_json_free(&json); }
int VFS_FS_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { int ret = SYSINFO_RET_FAIL; char line[MAX_STRING_LEN], *p, *mpoint, *mtype; FILE *f; struct zbx_json j; zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); if (NULL != (f = fopen("/proc/mounts", "r"))) { while (NULL != fgets(line, sizeof(line), f)) { if (NULL == (p = strchr(line, ' '))) continue; mpoint = ++p; if (NULL == (p = strchr(mpoint, ' '))) continue; *p = '\0'; mtype = ++p; if (NULL == (p = strchr(mtype, ' '))) continue; *p = '\0'; zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#FSNAME}", mpoint, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#FSTYPE}", mtype, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); } zbx_fclose(f); ret = SYSINFO_RET_OK; } zbx_json_close(&j); SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer)); zbx_json_free(&j); return ret; }
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; }
/****************************************************************************** * * * Function: send_proxyhistory * * * * Purpose: send history data to a Zabbix server * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void send_proxyhistory(zbx_sock_t *sock) { const char *__function_name = "send_proxyhistory"; struct zbx_json j; zbx_uint64_t lastid; int records; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); records = proxy_get_hist_data(&j, &lastid); zbx_json_close(&j); zbx_json_adduint64(&j, ZBX_PROTO_TAG_CLOCK, (int)time(NULL)); if (FAIL == zbx_tcp_send_to(sock, j.buffer, CONFIG_TIMEOUT)) zabbix_log(LOG_LEVEL_WARNING, "Error while sending availability of hosts. %s", zbx_tcp_strerror()); else if (SUCCEED == zbx_recv_response(sock, NULL, 0, CONFIG_TIMEOUT) && 0 != records) proxy_set_hist_lastid(lastid); zbx_json_free(&j); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
int check_vcenter_hv_datastore_discovery(AGENT_REQUEST *request, const char *username, const char *password, AGENT_RESULT *result) { char *url, *uuid; zbx_vmware_service_t *service; zbx_vmware_hv_t *hv; struct zbx_json json_data; int i, ret = SYSINFO_RET_FAIL; if (2 != request->nparam) return SYSINFO_RET_FAIL; url = get_rparam(request, 0); uuid = get_rparam(request, 1); zbx_vmware_lock(); if (NULL == (service = get_vmware_service(url, username, password, result, &ret))) goto unlock; if (NULL == (hv = hv_get(&service->data->hvs, uuid))) goto unlock; zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA); for (i = 0; i < hv->datastores.values_num; i++) { zbx_vmware_datastore_t *datastore = hv->datastores.values[i]; zbx_json_addobject(&json_data, NULL); zbx_json_addstring(&json_data, "{#DATASTORE}", datastore->name, ZBX_JSON_TYPE_STRING); zbx_json_close(&json_data); } zbx_json_close(&json_data); SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer)); zbx_json_free(&json_data); ret = SYSINFO_RET_OK; unlock: zbx_vmware_unlock(); return ret; }
int VFS_FS_DISCOVERY(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { int rc, sz, i, ret = SYSINFO_RET_FAIL; struct vmount *vm = NULL; struct zbx_json j; /* check how many bytes to allocate for the mounted filesystems */ if (-1 == (rc = mntctl(MCTL_QUERY, sizeof(sz), (char *)&sz))) return ret; vm = zbx_malloc(vm, (size_t)sz); /* get the list of mounted filesystems */ /* return code is number of filesystems returned */ if (-1 == (rc = mntctl(MCTL_QUERY, sz, (char *)vm))) goto error; 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}", (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(vm); return ret; }
int check_vcenter_cluster_discovery(AGENT_REQUEST *request, const char *username, const char *password, AGENT_RESULT *result) { struct zbx_json json_data; int i, ret = SYSINFO_RET_FAIL; char *url; zbx_vmware_service_t *service; if (1 != request->nparam) return SYSINFO_RET_FAIL; url = get_rparam(request, 0); zbx_vmware_lock(); if (NULL == (service = get_vmware_service(url, username, password, result, &ret))) goto unlock; zbx_json_init(&json_data, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&json_data, ZBX_PROTO_TAG_DATA); for (i = 0; i < service->data->clusters.values_num; i++) { zbx_vmware_cluster_t *cluster = (zbx_vmware_cluster_t *)service->data->clusters.values[i]; zbx_json_addobject(&json_data, NULL); zbx_json_addstring(&json_data, "{#CLUSTER.ID}", cluster->id, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json_data, "{#CLUSTER.NAME}", cluster->name, ZBX_JSON_TYPE_STRING); zbx_json_close(&json_data); } zbx_json_close(&json_data); SET_STR_RESULT(result, zbx_strdup(NULL, json_data.buffer)); zbx_json_free(&json_data); ret = SYSINFO_RET_OK; unlock: zbx_vmware_unlock(); return ret; }
int NET_IF_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { char line[MAX_STRING_LEN], *p; FILE *f; struct zbx_json j; if (NULL == (f = fopen("/proc/net/dev", "r"))) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc/net/dev: %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); while (NULL != fgets(line, sizeof(line), f)) { if (NULL == (p = strstr(line, ":"))) continue; *p = '\0'; /* trim left spaces */ for (p = line; ' ' == *p && '\0' != *p; p++) ; zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#IFNAME}", p, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); } zbx_fclose(f); zbx_json_close(&j); SET_STR_RESULT(result, strdup(j.buffer)); zbx_json_free(&j); return SYSINFO_RET_OK; }
int NET_IF_DISCOVERY(const char *cmd, const char *param, unsigned flags, AGENT_RESULT *result) { int ret = SYSINFO_RET_FAIL; char line[MAX_STRING_LEN], *p; FILE *f; struct zbx_json j; zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); if (NULL != (f = fopen("/proc/net/dev", "r"))) { while (NULL != fgets(line, sizeof(line), f)) { if (NULL == (p = strstr(line, ":"))) continue; *p = '\0'; /* trim left spaces */ for (p = line; ' ' == *p && '\0' != *p; p++) ; zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#IFNAME}", p, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); } zbx_fclose(f); ret = SYSINFO_RET_OK; } zbx_json_close(&j); SET_STR_RESULT(result, strdup(j.buffer)); zbx_json_free(&j); return ret; }
int SYSTEM_CPU_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { int i; zbx_vector_uint64_t cpus; struct zbx_json json; zbx_vector_uint64_create(&cpus); if (SUCCEED != get_cpu_statuses(&cpus)) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Collector in not started.")); zbx_vector_uint64_destroy(&cpus); return SYSINFO_RET_FAIL; } zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA); for (i = 0; i < cpus.values_num; i++) { zbx_json_addobject(&json, NULL); zbx_json_adduint64(&json, "{#CPU.NUMBER}", i); zbx_json_addstring(&json, "{#CPU.STATUS}", (PERF_COUNTER_ACTIVE == cpus.values[i]) ? "online" : (PERF_COUNTER_INITIALIZED == cpus.values[i]) ? "unknown" : "offline", ZBX_JSON_TYPE_STRING); zbx_json_close(&json); } zbx_json_close(&json); SET_STR_RESULT(result, zbx_strdup(result->str, json.buffer)); zbx_json_free(&json); zbx_vector_uint64_destroy(&cpus); return SYSINFO_RET_OK; }
/****************************************************************************** * * * Function: send_host_availability * * * * Purpose: send hosts availability data from proxy * * * ******************************************************************************/ void send_host_availability(zbx_socket_t *sock) { const char *__function_name = "send_host_availability"; struct zbx_json j; int ret = FAIL, ts; char *error = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (SUCCEED != check_access_passive_proxy(sock, ZBX_DO_NOT_SEND_RESPONSE, "host availability data request")) { /* do not send any reply to server in this case as the server expects host availability data */ goto out1; } zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); /* if there are no host availability changes we still have to send empty data in response */ if (SUCCEED != get_host_availability_data(&j, &ts)) { zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); zbx_json_close(&j); } zabbix_log(LOG_LEVEL_DEBUG, "%s() [%s]", __function_name, j.buffer); if (SUCCEED != zbx_tcp_send_to(sock, j.buffer, CONFIG_TIMEOUT)) { error = zbx_strdup(error, zbx_socket_strerror()); goto out; } if (SUCCEED != zbx_recv_response(sock, CONFIG_TIMEOUT, &error)) goto out; zbx_set_availability_diff_ts(ts); ret = SUCCEED; out: if (SUCCEED != ret) { zabbix_log(LOG_LEVEL_WARNING, "cannot send host availability data to server at \"%s\": %s", sock->peer, error); } zbx_json_free(&j); zbx_free(error); out1: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: history_sender * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void history_sender(struct zbx_json *j, int *records, const char *tag, int (*f_get_data)(), void (*f_set_lastid)()) { const char *__function_name = "history_sender"; zbx_sock_t sock; zbx_uint64_t lastid; zbx_timespec_t ts; int ret = SUCCEED; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_json_clean(j); zbx_json_addstring(j, ZBX_PROTO_TAG_REQUEST, tag, ZBX_JSON_TYPE_STRING); zbx_json_addstring(j, ZBX_PROTO_TAG_HOST, CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); zbx_json_addarray(j, ZBX_PROTO_TAG_DATA); *records = f_get_data(j, &lastid); zbx_json_close(j); if (*records > 0) { char *error = NULL; connect_to_server(&sock, 600, CONFIG_PROXYDATA_FREQUENCY); /* retry till have a connection */ zbx_timespec(&ts); zbx_json_adduint64(j, ZBX_PROTO_TAG_CLOCK, ts.sec); zbx_json_adduint64(j, ZBX_PROTO_TAG_NS, ts.ns); if (SUCCEED != (ret = put_data_to_server(&sock, j, &error))) { *records = 0; zabbix_log(LOG_LEVEL_WARNING, "sending data to server failed: %s", error); } zbx_free(error); disconnect_server(&sock); } if (SUCCEED == ret && 0 != lastid) { DBbegin(); f_set_lastid(lastid); DBcommit(); } zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: send_proxyhistory * * * * Purpose: send history data to a Zabbix server * * * ******************************************************************************/ static void send_proxyhistory(zbx_socket_t *sock, zbx_timespec_t *ts) { const char *__function_name = "send_proxyhistory"; struct zbx_json j; zbx_uint64_t lastid; int records, ret = FAIL; char *error = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); if (SUCCEED != check_access_passive_proxy(sock, ZBX_DO_NOT_SEND_RESPONSE, "history data request")) { /* do not send any reply to server in this case as the server expects history data */ goto out1; } zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); records = proxy_get_hist_data(&j, &lastid); zbx_json_close(&j); zbx_json_adduint64(&j, ZBX_PROTO_TAG_CLOCK, ts->sec); zbx_json_adduint64(&j, ZBX_PROTO_TAG_NS, ts->ns); if (SUCCEED != zbx_tcp_send_to(sock, j.buffer, CONFIG_TIMEOUT)) { error = zbx_strdup(error, zbx_socket_strerror()); goto out; } if (SUCCEED != zbx_recv_response(sock, CONFIG_TIMEOUT, &error)) goto out; if (0 != records) proxy_set_hist_lastid(lastid); ret = SUCCEED; out: if (SUCCEED != ret) zabbix_log(LOG_LEVEL_WARNING, "cannot send history data to server at \"%s\": %s", sock->peer, error); zbx_json_free(&j); zbx_free(error); out1: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
int VFS_FS_DISCOVERY(AGENT_REQUEST *request, AGENT_RESULT *result) { int ret = SYSINFO_RET_FAIL; struct mnttab mt; FILE *f; struct zbx_json j; zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); /* opening the mounted filesystems file */ if (NULL != (f = fopen("/etc/mnttab", "r"))) { /* fill mnttab structure from file */ while (-1 != getmntent(f, &mt)) { zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#FSNAME}", mt.mnt_mountp, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#FSTYPE}", mt.mnt_fstype, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); } zbx_fclose(f); ret = SYSINFO_RET_OK; } zbx_json_close(&j); SET_STR_RESULT(result, zbx_strdup(NULL, j.buffer)); zbx_json_free(&j); return ret; }
/****************************************************************************** * * * Function: queue_stats_export * * * * Purpose: export queue stats to JSON format * * * * Parameters: queue_stats - [IN] a hashset containing item stats * * id_name - [IN] the name of stats id field * * json - [OUT] the output JSON * * * ******************************************************************************/ static void queue_stats_export(zbx_hashset_t *queue_stats, const char *id_name, struct zbx_json *json) { zbx_hashset_iter_t iter; zbx_queue_stats_t *stats; zbx_json_addarray(json, ZBX_PROTO_TAG_DATA); zbx_hashset_iter_reset(queue_stats, &iter); while (NULL != (stats = zbx_hashset_iter_next(&iter))) { zbx_json_addobject(json, NULL); zbx_json_adduint64(json, id_name, stats->id); zbx_json_adduint64(json, "delay5", stats->delay5); zbx_json_adduint64(json, "delay10", stats->delay10); zbx_json_adduint64(json, "delay30", stats->delay30); zbx_json_adduint64(json, "delay60", stats->delay60); zbx_json_adduint64(json, "delay300", stats->delay300); zbx_json_adduint64(json, "delay600", stats->delay600); zbx_json_close(json); } zbx_json_close(json); }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ void reply_jobnetrun_response(zbx_sock_t * sock, int ret, char *message) { struct zbx_json json; /*JSON Data */ zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&json, JA_PROTO_TAG_KIND, JA_PROTO_VALUE_JOBNETRUN_RES, ZBX_JSON_TYPE_STRING); zbx_json_adduint64(&json, JA_PROTO_TAG_VERSION, JA_PROTO_VALUE_VERSION_1); /*data version */ zbx_json_addstring(&json, JA_PROTO_TAG_SERVERID, serverid, ZBX_JSON_TYPE_STRING); zbx_json_addobject(&json, JA_PROTO_TAG_DATA); zbx_json_adduint64(&json, JA_PROTO_TAG_RESULT, FAIL == ret ? 1 : 0); zbx_json_addstring(&json, JA_PROTO_TAG_MESSAGE, message, ZBX_JSON_TYPE_STRING); zbx_json_close(&json); zbx_json_close(&json); zabbix_log(LOG_LEVEL_DEBUG, "JSON before sending [%s]", json.buffer); if (FAIL == (ret = zbx_tcp_send_to(sock, json.buffer, CONFIG_TIMEOUT))) { ja_log("JATRAPPER200038", 0, NULL, 0, zbx_tcp_strerror()); ret = NETWORK_ERROR; } zbx_json_free(&json); }
/****************************************************************************** * * * Function: send_areg_data * * * * Purpose: send auto-registration data from proxy to a server * * * * Author: Alexander Vladishev * * * ******************************************************************************/ void send_areg_data(zbx_sock_t *sock) { const char *__function_name = "send_areg_data"; struct zbx_json j; zbx_uint64_t lastid; int records; char *info = NULL, *error = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); records = proxy_get_areg_data(&j, &lastid); zbx_json_close(&j); zbx_json_adduint64(&j, ZBX_PROTO_TAG_CLOCK, (int)time(NULL)); if (SUCCEED != zbx_tcp_send_to(sock, j.buffer, CONFIG_TIMEOUT)) { zabbix_log(LOG_LEVEL_WARNING, "error while sending auto-registration data to server: %s", zbx_tcp_strerror()); goto out; } if (SUCCEED != zbx_recv_response(sock, &info, CONFIG_TIMEOUT, &error)) { zabbix_log(LOG_LEVEL_WARNING, "sending auto-registration data to server: error:\"%s\", info:\"%s\"", ZBX_NULL2EMPTY_STR(error), ZBX_NULL2EMPTY_STR(info)); goto out; } if (0 != records) proxy_set_areg_lastid(lastid); out: zbx_json_free(&j); zbx_free(info); zbx_free(error); zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
int zbx_module_redis_discovery(AGENT_REQUEST *request, AGENT_RESULT *result) { char *p; struct zbx_json j; char *config = NULL; char *f = NULL; char *s = NULL; zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&j, ZBX_PROTO_TAG_DATA); /* for dev zabbix_log(LOG_LEVEL_INFORMATION, "module [redis], func [zbx_module_redis_discovery], CONFIG_REDIS_INSTANCE_PORT value is: [%s]", CONFIG_REDIS_INSTANCE_PORT); */ if (NULL != CONFIG_REDIS_INSTANCE_PORT && '\0' != *CONFIG_REDIS_INSTANCE_PORT) { config = zbx_strdup(NULL, CONFIG_REDIS_INSTANCE_PORT); p = strtok(config, ","); while (NULL != p) { f = p; if (NULL == (s = strchr(p, ':'))) { zbx_lrtrim(f, ZBX_CFG_RTRIM_CHARS); zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#RSHOST}", REDIS_DEFAULT_INSTANCE_HOST, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#RSPORT}", f, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); /* for dev zabbix_log(LOG_LEVEL_INFORMATION, "module [redis], func [zbx_module_redis_discovery], add instance:[%s:%s] to discovery list", REDIS_DEFAULT_INSTANCE_HOST, f); */ } else { *s++ = '\0'; zbx_lrtrim(f, ZBX_CFG_RTRIM_CHARS); zbx_lrtrim(s, ZBX_CFG_RTRIM_CHARS); if (NULL != s && '\0' != s) { zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#RSHOST}", f, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#RSPORT}", s, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); /* for dev zabbix_log(LOG_LEVEL_INFORMATION, "module [redis], func [zbx_module_redis_discovery], add instance:[%s:%s] to discovery list", f, s); */ } else { zbx_json_addobject(&j, NULL); zbx_json_addstring(&j, "{#RSHOST}", REDIS_DEFAULT_INSTANCE_HOST, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "{#RSPORT}", f, ZBX_JSON_TYPE_STRING); zbx_json_close(&j); /* for dev zabbix_log(LOG_LEVEL_INFORMATION, "module [redis], func [zbx_module_redis_discovery], add instance:[%s:%s] to discovery list", REDIS_DEFAULT_INSTANCE_HOST, f); */ } } p = strtok (NULL, ","); } } zbx_json_close(&j); SET_STR_RESULT(result, strdup(j.buffer)); zbx_json_free(&j); return SYSINFO_RET_OK; }
static int db_odbc_discovery(DC_ITEM *item, AGENT_REQUEST *request, AGENT_RESULT *result) { const char *__function_name = "db_odbc_discovery"; int ret = NOTSUPPORTED, i, j; ZBX_ODBC_DBH dbh; ZBX_ODBC_ROW row; char **columns, *p, macro[MAX_STRING_LEN]; struct zbx_json json; zabbix_log(LOG_LEVEL_DEBUG, "In %s() query:'%s'", __function_name, item->params); if (2 != request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters.")); goto out; } if (SUCCEED != odbc_DBconnect(&dbh, request->params[1], item->username, item->password, CONFIG_TIMEOUT)) { SET_MSG_RESULT(result, zbx_strdup(NULL, get_last_odbc_strerror())); goto out; } if (NULL != odbc_DBselect(&dbh, item->params)) { columns = zbx_malloc(NULL, sizeof(char *) * dbh.col_num); if (SUCCEED == get_result_columns(&dbh, columns)) { for (i = 0; i < dbh.col_num; i++) zabbix_log(LOG_LEVEL_DEBUG, "%s() column[%d]:'%s'", __function_name, i + 1, columns[i]); for (i = 0; i < dbh.col_num; i++) { for (p = columns[i]; '\0' != *p; p++) { if (0 != isalpha((unsigned char)*p)) *p = toupper((unsigned char)*p); if (SUCCEED != is_macro_char(*p)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot convert column #%d name to macro.", i + 1)); goto clean; } } for (j = 0; j < i; j++) { if (0 == strcmp(columns[i], columns[j])) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Duplicate macro name: {#%s}.", columns[i])); goto clean; } } } zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA); while (NULL != (row = odbc_DBfetch(&dbh))) { zbx_json_addobject(&json, NULL); for (i = 0; i < dbh.col_num; i++) { zbx_snprintf(macro, MAX_STRING_LEN, "{#%s}", columns[i]); zbx_json_addstring(&json, macro, row[i], ZBX_JSON_TYPE_STRING); } zbx_json_close(&json); } zbx_json_close(&json); SET_STR_RESULT(result, zbx_strdup(NULL, json.buffer)); zbx_json_free(&json); ret = SUCCEED; clean: for (i = 0; i < dbh.col_num; i++) zbx_free(columns[i]); } else SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot obtain column names.")); zbx_free(columns); } else SET_MSG_RESULT(result, zbx_strdup(NULL, get_last_odbc_strerror())); odbc_DBclose(&dbh); out: zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
/****************************************************************************** * * * Function: send_list_of_active_checks_json * * * * Purpose: send list of active checks to the host * * * * Parameters: sock - open socket of server-agent connection * * json - request buffer * * * * Return value: SUCCEED - list of active checks sent succesfully * * FAIL - an error occured * * * * Author: Aleksander Vladishev * * * * Comments: * * * ******************************************************************************/ int send_list_of_active_checks_json(zbx_sock_t *sock, struct zbx_json_parse *jp) { char host[HOST_HOST_LEN_MAX], *name_esc, params[MAX_STRING_LEN], pattern[MAX_STRING_LEN], tmp[32]; DB_RESULT result; DB_ROW row; DB_ITEM item; struct zbx_json json; int res = FAIL; zbx_uint64_t hostid; char error[MAX_STRING_LEN]; char **regexp = NULL; int regexp_alloc = 32; int regexp_num = 0, n; char *sql = NULL; int sql_alloc = 2048; int sql_offset; zabbix_log(LOG_LEVEL_DEBUG, "In send_list_of_active_checks_json()"); if (FAIL == zbx_json_value_by_name(jp, ZBX_PROTO_TAG_HOST, host, sizeof(host))) { zbx_snprintf(error, MAX_STRING_LEN, "%s", zbx_json_strerror()); goto out; } if (FAIL == get_hostid_by_host(host, &hostid, error)) goto out; regexp = zbx_malloc(regexp, regexp_alloc); sql = zbx_malloc(sql, sql_alloc); name_esc = DBdyn_escape_string(host); sql_offset = 0; zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 1024, "select %s where i.hostid=h.hostid and h.status=%d and i.type=%d and h.hostid=" ZBX_FS_UI64 " and h.proxy_hostid=0", ZBX_SQL_ITEM_SELECT, HOST_STATUS_MONITORED, ITEM_TYPE_ZABBIX_ACTIVE, hostid); if (0 != CONFIG_REFRESH_UNSUPPORTED) zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 256, " and (i.status=%d or (i.status=%d and i.nextcheck<=%d))", ITEM_STATUS_ACTIVE, ITEM_STATUS_NOTSUPPORTED, time(NULL)); else zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 256, " and i.status=%d", ITEM_STATUS_ACTIVE); zbx_free(name_esc); zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE, ZBX_PROTO_VALUE_SUCCESS, ZBX_JSON_TYPE_STRING); zbx_json_addarray(&json, ZBX_PROTO_TAG_DATA); result = DBselect("%s", sql); while (NULL != (row = DBfetch(result))) { DBget_item_from_db(&item, row); zbx_json_addobject(&json, NULL); zbx_json_addstring(&json, ZBX_PROTO_TAG_KEY, item.key, ZBX_JSON_TYPE_STRING); if (0 != strcmp(item.key, item.key_orig)) zbx_json_addstring(&json, ZBX_PROTO_TAG_KEY_ORIG, item.key_orig, ZBX_JSON_TYPE_STRING); zbx_snprintf(tmp, sizeof(tmp), "%d", item.delay); zbx_json_addstring(&json, ZBX_PROTO_TAG_DELAY, tmp, ZBX_JSON_TYPE_STRING); zbx_snprintf(tmp, sizeof(tmp), "%d", item.lastlogsize); zbx_json_addstring(&json, ZBX_PROTO_TAG_LOGLASTSIZE, tmp, ZBX_JSON_TYPE_STRING); zbx_json_close(&json); /* Special processing for log[] and eventlog[] items */ do { /* simple try realization */ if (0 != strncmp(item.key, "log[", 4) && 0 != strncmp(item.key, "eventlog[", 9)) break; if (2 != parse_command(item.key, NULL, 0, params, MAX_STRING_LEN)) break;; if (0 != get_param(params, 2, pattern, sizeof(pattern))) break; if (*pattern != '@') break; for (n = 0; n < regexp_num; n++) if (0 == strcmp(regexp[n], pattern + 1)) break; if (n != regexp_num) break; if (regexp_num == regexp_alloc) { regexp_alloc += 32; regexp = zbx_realloc(regexp, regexp_alloc); } regexp[regexp_num++] = strdup(pattern + 1); } while (0); /* simple try realization */ } zbx_json_close(&json); DBfree_result(result); if (0 != regexp_num) { zbx_json_addarray(&json, ZBX_PROTO_TAG_REGEXP); sql_offset = 0; zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 512, "select r.name,e.expression,e.expression_type,e.exp_delimiter,e.case_sensitive" " from regexps r,expressions e where r.regexpid=e.regexpid and r.name in ("); for (n = 0; n < regexp_num; n++) { name_esc = DBdyn_escape_string(regexp[n]); zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 512, "%s'%s'", n == 0 ? "" : ",", name_esc); zbx_free(name_esc); zbx_free(regexp[n]); } zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset, 8, ")"); result = DBselect("%s", sql); while (NULL != (row = DBfetch(result))) { zbx_json_addobject(&json, NULL); zbx_json_addstring(&json, "name", row[0], ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, "expression", row[1], ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, "expression_type", row[2], ZBX_JSON_TYPE_INT); zbx_json_addstring(&json, "exp_delimiter", row[3], ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, "case_sensitive", row[4], ZBX_JSON_TYPE_INT); zbx_json_close(&json); } DBfree_result(result); } zbx_free(regexp); zabbix_log(LOG_LEVEL_DEBUG, "Sending [%s]", json.buffer); if (SUCCEED != zbx_tcp_send_raw(sock, json.buffer)) zbx_snprintf(error, MAX_STRING_LEN, "%s", zbx_tcp_strerror()); else res = SUCCEED; zbx_json_free(&json); zbx_free(sql); out: if (FAIL == res) zabbix_log(LOG_LEVEL_WARNING, "Send list of active checks to [%s] failed: %s", get_ip_by_socket(sock), error); return res; }