/****************************************************************************** * * * Function: zbx_send_response * * * * Purpose: send json SUCCEED or FAIL to socket along with an info message * * * * Parameters: sock - [IN] socket descriptor * * result - [IN] SUCCEED or FAIL * * info - [IN] info message * * timeout - [IN] timeout for this operation * * * * Return value: SUCCEED - data successfully transmited * * NETWORK_ERROR - network related error occurred * * * * Author: Alexander Vladishev, Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ int zbx_send_response_ext(zbx_socket_t *sock, int result, const char *info, int protocol, int timeout) { const char *__function_name = "zbx_send_response"; struct zbx_json json; const char *resp; int ret = SUCCEED; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); resp = SUCCEED == result ? ZBX_PROTO_VALUE_SUCCESS : ZBX_PROTO_VALUE_FAILED; zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE, resp, ZBX_JSON_TYPE_STRING); if (NULL != info && '\0' != *info) zbx_json_addstring(&json, ZBX_PROTO_TAG_INFO, info, ZBX_JSON_TYPE_STRING); zabbix_log(LOG_LEVEL_DEBUG, "%s() '%s'", __function_name, json.buffer); if (FAIL == (ret = zbx_tcp_send_ext(sock, json.buffer, strlen(json.buffer), protocol, timeout))) { zabbix_log(LOG_LEVEL_DEBUG, "Error sending result back: %s", zbx_socket_strerror()); ret = NETWORK_ERROR; } zbx_json_free(&json); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); 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; }
/****************************************************************************** * * * Function: host_availability_sender * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void host_availability_sender(struct zbx_json *j) { const char *__function_name = "host_availability_sender"; zbx_sock_t sock; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); zbx_json_clean(j); zbx_json_addstring(j, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_HOST_AVAILABILITY, ZBX_JSON_TYPE_STRING); zbx_json_addstring(j, ZBX_PROTO_TAG_HOST, CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); if (SUCCEED == get_host_availability_data(j)) { char *error = NULL; connect_to_server(&sock, 600, CONFIG_PROXYDATA_FREQUENCY); /* retry till have a connection */ if (SUCCEED != put_data_to_server(&sock, j, &error)) zabbix_log(LOG_LEVEL_WARNING, "sending host availability data to server failed: %s", error); zbx_free(error); disconnect_server(&sock); } zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
/****************************************************************************** * * * Function: send_heartbeat * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static int send_heartbeat(void) { zbx_sock_t sock; struct zbx_json j; int ret = SUCCEED; char *error = NULL; zabbix_log(LOG_LEVEL_DEBUG, "In send_heartbeat()"); zbx_json_init(&j, 128); zbx_json_addstring(&j, "request", ZBX_PROTO_VALUE_PROXY_HEARTBEAT, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "host", CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); if (FAIL == connect_to_server(&sock, CONFIG_HEARTBEAT_FREQUENCY, 0)) /* do not retry */ return FAIL; if (SUCCEED != put_data_to_server(&sock, &j, &error)) { zabbix_log(LOG_LEVEL_WARNING, "sending heartbeat message to server failed: %s", error); ret = FAIL; } zbx_free(error); disconnect_server(&sock); return ret; }
/****************************************************************************** * * * Function: get_data_from_server * * * * Purpose: get configuration and other data from server * * * * Parameters: * * * * Return value: SUCCESS - processed successfully * * FAIL - an error occurred * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ int get_data_from_server(zbx_sock_t *sock, const char *request) { const char *__function_name = "get_data_from_server"; int ret = FAIL; struct zbx_json j; zabbix_log(LOG_LEVEL_DEBUG, "In %s() request:'%s'", __function_name, request); zbx_json_init(&j, 128); zbx_json_addstring(&j, "request", request, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "host", CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); if (FAIL == send_data_to_server(sock, j.buffer)) goto exit; if (FAIL == recv_data_from_server(sock)) goto exit; ret = SUCCEED; exit: zbx_json_free(&j); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); 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; }
/* * 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 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; }
/****************************************************************************** * * * 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); }
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; }
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(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; }
/****************************************************************************** * * * Function: get_data_from_proxy * * * * Purpose: get historical data from proxy * * * * Parameters: * * * * Return value: SUCCESS - processed successfully * * FAIL - an error occurred * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static int get_data_from_proxy(DC_PROXY *proxy, const char *request, char **data) { const char *__function_name = "get_data_from_proxy"; zbx_socket_t s; struct zbx_json j; int ret = FAIL; zabbix_log(LOG_LEVEL_DEBUG, "In %s() request:'%s'", __function_name, request); zbx_json_init(&j, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&j, "request", request, ZBX_JSON_TYPE_STRING); if (SUCCEED == (ret = connect_to_proxy(proxy, &s, CONFIG_TRAPPER_TIMEOUT))) { if (SUCCEED == (ret = send_data_to_proxy(proxy, &s, j.buffer))) if (SUCCEED == (ret = recv_data_from_proxy(proxy, &s))) if (SUCCEED == (ret = zbx_send_response(&s, SUCCEED, NULL, 0))) *data = zbx_strdup(*data, s.buffer); disconnect_proxy(&s); } zbx_json_free(&j); zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret)); return ret; }
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; }
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 }
/****************************************************************************** * * * Function: send_result * * * * Purpose: send json SUCCEED or FAIL to socket along with an info message * * * * Parameters: result SUCCEED or FAIL * * * * Return value: SUCCEED - processed successfully * * FAIL - an error occured * * * * Author: Alexei Vladishev * * * * Comments: * * * ******************************************************************************/ int send_result(zbx_sock_t *sock, int result, char *info) { int ret = SUCCEED; struct zbx_json json; zbx_json_init(&json, 1024); zbx_json_addstring(&json, ZBX_PROTO_TAG_RESPONSE, SUCCEED == result ? ZBX_PROTO_VALUE_SUCCESS : ZBX_PROTO_VALUE_FAILED, ZBX_JSON_TYPE_STRING); if(info != NULL && info[0]!='\0') { zbx_json_addstring(&json, ZBX_PROTO_TAG_INFO, info, ZBX_JSON_TYPE_STRING); } ret = zbx_tcp_send(sock, json.buffer); zbx_json_free(&json); 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_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; }
/****************************************************************************** * * * Function: send_heartbeat * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Author: Alexander Vladishev * * * * Comments: * * * ******************************************************************************/ static void send_heartbeat() { zbx_sock_t sock; struct zbx_json j; zabbix_log(LOG_LEVEL_DEBUG, "In send_heartbeat()"); zbx_json_init(&j, 128); zbx_json_addstring(&j, "request", ZBX_PROTO_VALUE_PROXY_HEARTBEAT, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "host", CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); if (FAIL == connect_to_server(&sock, CONFIG_HEARTBEAT_FREQUENCY, 0)) /* do not retry */ return; if (FAIL == put_data_to_server(&sock, &j)) zabbix_log(LOG_LEVEL_WARNING, "Heartbeat message failed"); disconnect_server(&sock); }
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; }
/****************************************************************************** * * * 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 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: refresh_active_checks * * * * Purpose: Retrive from ZABBIX server list of active checks * * * * Parameters: host - IP or Hostname of ZABBIX server * * port - port of ZABBIX server * * * * Return value: returns SUCCEED on succesfull parsing, * * FAIL on other cases * * * * Author: Eugene Grigorjev, Alexei Vladishev (new json protocol) * * * * Comments: * * * ******************************************************************************/ static int refresh_active_checks(const char *host, unsigned short port) { zbx_sock_t s; char *buf; int ret; struct zbx_json json; zabbix_log( LOG_LEVEL_DEBUG, "refresh_active_checks('%s',%u)", host, port); zbx_json_init(&json, ZBX_JSON_STAT_BUF_LEN); zbx_json_addstring(&json, ZBX_PROTO_TAG_REQUEST, ZBX_PROTO_VALUE_GET_ACTIVE_CHECKS, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&json, ZBX_PROTO_TAG_HOST, CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); if (SUCCEED == (ret = zbx_tcp_connect(&s, CONFIG_SOURCE_IP, host, port, CONFIG_TIMEOUT))) { zabbix_log(LOG_LEVEL_DEBUG, "Sending [%s]", json.buffer); if (SUCCEED == (ret = zbx_tcp_send(&s, json.buffer))) { zabbix_log(LOG_LEVEL_DEBUG, "Before read"); if (SUCCEED == (ret = zbx_tcp_recv_ext(&s, &buf, ZBX_TCP_READ_UNTIL_CLOSE))) { zabbix_log(LOG_LEVEL_DEBUG, "Got [%s]", buf); parse_list_of_checks(buf); } } zbx_tcp_close(&s); } if (FAIL == ret) zabbix_log(LOG_LEVEL_DEBUG, "Get active checks error: %s", zbx_tcp_strerror()); zbx_json_free(&json); return ret; }
/****************************************************************************** * * * Function: get_data_from_server * * * * Purpose: get configuration and othed data from server * * * * Parameters: * * * * Return value: SUCCESS - processed succesfully * * FAIL - an error occured * * * * Author: Alksander Vladishev * * * * Comments: * * * ******************************************************************************/ int get_data_from_server(zbx_sock_t *sock, const char *request, char **data) { int ret = FAIL; struct zbx_json j; zabbix_log(LOG_LEVEL_DEBUG, "In get_data_from_server() [request:%s]", request); zbx_json_init(&j, 128); zbx_json_addstring(&j, "request", request, ZBX_JSON_TYPE_STRING); zbx_json_addstring(&j, "host", CONFIG_HOSTNAME, ZBX_JSON_TYPE_STRING); if (FAIL == send_data_to_server(sock, j.buffer)) goto exit; if (FAIL == recv_data_from_server(sock, data)) goto exit; ret = SUCCEED; exit: zbx_json_free(&j); return ret; }
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: * * * * 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); }
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 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; }