/****************************************************************************** * * * Function: read_traps * * * * Purpose: read the traps and then parse them with parse_traps() * * * * Author: Rudolfs Kreicbergs * * * ******************************************************************************/ static int read_traps() { const char *__function_name = "read_traps"; int nbytes = 0; zabbix_log(LOG_LEVEL_DEBUG, "In %s() lastsize:%d", __function_name, trap_lastsize); if ((off_t)-1 == lseek(trap_fd, (off_t)trap_lastsize, SEEK_SET)) { zabbix_log(LOG_LEVEL_WARNING, "cannot set position to %d for \"%s\": %s", trap_lastsize, CONFIG_SNMPTRAP_FILE, zbx_strerror(errno)); goto exit; } if (-1 == (nbytes = read(trap_fd, buffer + offset, MAX_BUFFER_LEN - offset - 1))) { zabbix_log(LOG_LEVEL_WARNING, "cannot read from SNMP trapper file \"%s\": %s", CONFIG_SNMPTRAP_FILE, zbx_strerror(errno)); goto exit; } if (0 < nbytes) { if (INT_MAX < (zbx_uint64_t)trap_lastsize + nbytes) { nbytes = 0; goto exit; } buffer[nbytes + offset] = '\0'; trap_lastsize += nbytes; DBupdate_lastsize(); parse_traps(0); } exit: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); return nbytes; }
static int VM_MEMORY_USED(AGENT_RESULT *result) { struct sysinfo info; if (0 != sysinfo(&info)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } SET_UI64_RESULT(result, (zbx_uint64_t)(info.totalram - info.freeram) * info.mem_unit); return SYSINFO_RET_OK; }
int SYSTEM_SW_ARCH(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_strdup(NULL, name.machine)); return SYSINFO_RET_OK; }
/****************************************************************************** * * * Function: read_traps * * * * Purpose: read the traps and then parse them with parse_traps() * * * * Author: Rudolfs Kreicbergs * * * ******************************************************************************/ static void read_traps() { const char *__function_name = "read_traps"; int nbytes; char buffer[MAX_BUFFER_LEN]; zabbix_log(LOG_LEVEL_DEBUG, "In %s() lastsize:%d", __function_name, trap_lastsize); *buffer = '\0'; if ((off_t)-1 == lseek(trap_fd, (off_t)trap_lastsize, SEEK_SET)) { zabbix_log(LOG_LEVEL_WARNING, "cannot set position to [%d] for [%s]: %s", trap_lastsize, CONFIG_SNMPTRAP_FILE, zbx_strerror(errno)); goto exit; } if (-1 == (nbytes = read(trap_fd, buffer, sizeof(buffer) - 1))) { zabbix_log(LOG_LEVEL_WARNING, "cannot read from [%s]: %s", CONFIG_SNMPTRAP_FILE, zbx_strerror(errno)); goto exit; } if (0 < nbytes) { buffer[nbytes] = '\0'; zbx_rtrim(buffer + MAX(nbytes - 3, 0), " \r\n"); trap_lastsize += nbytes; DBupdate_lastsize(); parse_traps(buffer); } exit: zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
int SYSTEM_SWAP_SIZE(AGENT_REQUEST *request, AGENT_RESULT *result) { #ifdef HAVE_LIBPERFSTAT perfstat_memory_total_t mem; char *swapdev, *mode; if (2 < request->nparam) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters.")); return SYSINFO_RET_FAIL; } swapdev = get_rparam(request, 0); mode = get_rparam(request, 1); if (NULL != swapdev && '\0' != *swapdev && 0 != strcmp(swapdev, "all")) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid first parameter.")); return SYSINFO_RET_FAIL; } if (1 != perfstat_memory_total(NULL, &mem, sizeof(perfstat_memory_total_t), 1)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } if (NULL == mode || '\0' == *mode || 0 == strcmp(mode, "free")) SET_UI64_RESULT(result, mem.pgsp_free << ZBX_PERFSTAT_PAGE_SHIFT); else if (0 == strcmp(mode, "total")) SET_UI64_RESULT(result, mem.pgsp_total << ZBX_PERFSTAT_PAGE_SHIFT); else if (0 == strcmp(mode, "used")) SET_UI64_RESULT(result, (mem.pgsp_total - mem.pgsp_free) << ZBX_PERFSTAT_PAGE_SHIFT); else if (0 == strcmp(mode, "pfree")) SET_DBL_RESULT(result, mem.pgsp_total ? 100.0 * (mem.pgsp_free / (double)mem.pgsp_total) : 0.0); else if (0 == strcmp(mode, "pused")) SET_DBL_RESULT(result, mem.pgsp_total ? 100.0 - 100.0 * (mem.pgsp_free / (double)mem.pgsp_total) : 0.0); else { SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid second parameter.")); return SYSINFO_RET_FAIL; } 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 }
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; }
int KERNEL_MAXPROC(AGENT_REQUEST *request, AGENT_RESULT *result) { int mib[] = {CTL_KERN, KERN_MAXPROC}, maxproc; size_t len = sizeof(maxproc); if (0 != sysctl(mib, 2, &maxproc, &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, maxproc); return SYSINFO_RET_OK; }
/****************************************************************************** * * * Function: free_collector_data * * * * Purpose: Free memory allocated for collector * * * * Author: Eugene Grigorjev * * * * Comments: Unix version allocated memory as shared. * * * ******************************************************************************/ void free_collector_data() { #ifdef _WINDOWS zbx_free(collector); #else if (NULL == collector) return; if (NONEXISTENT_SHMID != collector->diskstat_shmid) { if (-1 == shmctl(collector->diskstat_shmid, IPC_RMID, 0)) zabbix_log(LOG_LEVEL_WARNING, "cannot remove shared memory for disk statistics collector: %s", zbx_strerror(errno)); diskdevices = NULL; collector->diskstat_shmid = NONEXISTENT_SHMID; } if (-1 == shmctl(shm_id, IPC_RMID, 0)) zabbix_log(LOG_LEVEL_WARNING, "cannot remove shared memory for collector: %s", zbx_strerror(errno)); zbx_mutex_destroy(&diskstats_lock); #endif collector = NULL; }
int php_sem_remove(PHP_MUTEX *sem_ptr) { const char *__function_name = "php_sem_remove"; union semun un; struct semid_ds buf; struct sembuf sop[2]; int opcnt = 1; assert(sem_ptr); if (-1 == sem_ptr->semid) return PHP_MUTEX_OK; /* Decrement the usage count. */ sop[0].sem_num = SYSVSEM_USAGE; sop[0].sem_op = -1; sop[0].sem_flg = SEM_UNDO; if (sem_ptr->count) { sop[1].sem_num = SYSVSEM_SEM; sop[1].sem_op = sem_ptr->count; sop[1].sem_flg = SEM_UNDO; opcnt++; } if (-1 == semop(sem_ptr->semid, sop, opcnt)) { zbx_error("%s(): failed for (id %d): %s", __function_name, sem_ptr->semid, zbx_strerror(errno)); return PHP_MUTEX_ERROR; } un.buf = &buf; if (-1 == semctl(sem_ptr->semid, 0, IPC_STAT, un)) { zbx_error("%s(): SysV semaphore (id %d) does not (any longer) exist", __function_name, sem_ptr->semid); return PHP_MUTEX_ERROR; } if (-1 == semctl(sem_ptr->semid, 0, IPC_RMID, un)) return PHP_MUTEX_ERROR; sem_ptr->semid = -1; return PHP_MUTEX_OK; }
int SYSTEM_UPTIME(AGENT_REQUEST *request, AGENT_RESULT *result) { struct sysinfo info; ZBX_UNUSED(request); if (0 != sysinfo(&info)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } SET_UI64_RESULT(result, info.uptime); return SYSINFO_RET_OK; }
static int VM_MEMORY_SHARED(AGENT_RESULT *result) { struct vmtotal vm; size_t len = sizeof(vm); int mib[] = {CTL_VM, VM_METER}; if (0 != sysctl(mib, 2, &vm, &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, (zbx_uint64_t)(vm.t_vmshr + vm.t_rmshr) * pagesize); return SYSINFO_RET_OK; }
/****************************************************************************** * * * Function: user1_signal_handler * * * * Purpose: handle user signal SIGUSR1 * * * ******************************************************************************/ static void user1_signal_handler(int sig, siginfo_t *siginfo, void *context) { SIG_CHECK_PARAMS(sig, siginfo, context); zabbix_log(LOG_LEVEL_DEBUG, "Got signal [signal:%d(%s),sender_pid:%d,sender_uid:%d,value_int:%d].", sig, get_signal_name(sig), SIG_CHECKED_FIELD(siginfo, si_pid), SIG_CHECKED_FIELD(siginfo, si_uid), SIG_CHECKED_FIELD(siginfo, si_value.ZBX_SIVAL_INT)); #ifdef HAVE_SIGQUEUE if (!SIG_PARENT_PROCESS) { extern void zbx_sigusr_handler(zbx_task_t task); zbx_sigusr_handler(SIG_CHECKED_FIELD(siginfo, si_value.ZBX_SIVAL_INT)); } else if (ZBX_TASK_CONFIG_CACHE_RELOAD == SIG_CHECKED_FIELD(siginfo, si_value.ZBX_SIVAL_INT)) { extern unsigned char daemon_type; if (0 != (daemon_type & ZBX_DAEMON_TYPE_PROXY_PASSIVE)) { zabbix_log(LOG_LEVEL_WARNING, "forced reloading of the configuration cache" " cannot be performed for a passive proxy"); } else { union sigval s; extern pid_t *threads; s.ZBX_SIVAL_INT = ZBX_TASK_CONFIG_CACHE_RELOAD; /* threads[0] is configuration syncer (it is set in proxy.c and server.c) */ if (NULL != threads && -1 != sigqueue(threads[0], SIGUSR1, s)) { zabbix_log(LOG_LEVEL_DEBUG, "the signal is redirected to" " the configuration syncer"); } else { zabbix_log(LOG_LEVEL_ERR, "failed to redirect signal: %s", zbx_strerror(errno)); } } } #endif }
void zbx_mem_destroy(zbx_mem_info_t *info) { const char *__function_name = "zbx_mem_destroy"; zabbix_log(LOG_LEVEL_DEBUG, "In %s() descr:'%s'", __function_name, info->mem_descr); if (info->use_lock) zbx_mutex_destroy(&info->mem_lock); if (-1 == shmctl(info->shm_id, IPC_RMID, 0)) { zabbix_log(LOG_LEVEL_WARNING, "cannot remove shared memory for %s: %s", info->mem_descr, zbx_strerror(errno)); } zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name); }
int read_pid_file(const char *pidfile, pid_t *pid, char *error, size_t max_error_len) { int ret = FAIL; FILE *fpid; if (NULL == (fpid = fopen(pidfile, "r"))) { zbx_snprintf(error, max_error_len, "cannot open PID file [%s]: %s", pidfile, zbx_strerror(errno)); return ret; } if (1 == fscanf(fpid, "%d", (int *)pid)) ret = SUCCEED; zbx_fclose(fpid); return ret; }
int SYSTEM_CPU_INTR(AGENT_REQUEST *request, AGENT_RESULT *result) { #ifdef HAVE_LIBPERFSTAT perfstat_cpu_total_t ps_cpu_total; 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, (zbx_uint64_t)ps_cpu_total.devintrs); 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 }
static int VM_MEMORY_SHARED(AGENT_RESULT *result) { #ifdef KERNEL_2_4 struct sysinfo info; if (0 != sysinfo(&info)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } SET_UI64_RESULT(result, (zbx_uint64_t)info.sharedram * info.mem_unit); return SYSINFO_RET_OK; #else SET_MSG_RESULT(result, zbx_strdup(NULL, "Supported for Linux 2.4 only.")); return SYSINFO_RET_FAIL; #endif }
static int get_fs_size_stat(const char *fs, zbx_uint64_t *total, zbx_uint64_t *free, zbx_uint64_t *used, double *pfree, double *pused, char **error) { #ifdef HAVE_SYS_STATVFS_H # define ZBX_STATFS statvfs # define ZBX_BSIZE f_frsize #else # define ZBX_STATFS statfs # define ZBX_BSIZE f_bsize #endif struct ZBX_STATFS s; if (0 != ZBX_STATFS(fs, &s)) { *error = zbx_dsprintf(NULL, "Cannot obtain filesystem information: %s", zbx_strerror(errno)); return SYSINFO_RET_FAIL; } if (total) *total = (zbx_uint64_t)s.f_blocks * s.ZBX_BSIZE; if (free) *free = (zbx_uint64_t)s.f_bavail * s.ZBX_BSIZE; if (used) *used = (zbx_uint64_t)(s.f_blocks - s.f_bfree) * s.ZBX_BSIZE; if (pfree) { if (0 != s.f_blocks - s.f_bfree + s.f_bavail) *pfree = (double)(100.0 * s.f_bavail) / (s.f_blocks - s.f_bfree + s.f_bavail); else *pfree = 0; } if (pused) { if (0 != s.f_blocks - s.f_bfree + s.f_bavail) *pused = 100.0 - (double)(100.0 * s.f_bavail) / (s.f_blocks - s.f_bfree + s.f_bavail); else *pused = 0; } return SYSINFO_RET_OK; }
static int get_swap_io(zbx_uint64_t *icount, zbx_uint64_t *ipages, zbx_uint64_t *ocount, zbx_uint64_t *opages, char **error) { int mib[2]; size_t len; struct uvmexp v; mib[0] = CTL_VM; mib[1] = VM_UVMEXP; len = sizeof(v); if (0 != sysctl(mib, 2, &v, &len, NULL, 0)) { *error = zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)); return SYSINFO_RET_FAIL; } /* int swapins; swapins */ /* int swapouts; swapouts */ /* int pgswapin; pages swapped in */ /* int pgswapout; pages swapped out */ #if OpenBSD < 201311 /* swapins and swapouts are not supported starting from OpenBSD 5.4 */ if (NULL != icount) *icount = (zbx_uint64_t)v.swapins; if (NULL != ocount) *ocount = (zbx_uint64_t)v.swapouts; #else if (NULL != icount || NULL != ocount) { *error = zbx_dsprintf(NULL, "Not supported by the system starting from OpenBSD 5.4."); return SYSINFO_RET_FAIL; } #endif if (NULL != ipages) *ipages = (zbx_uint64_t)v.pgswapin; if (NULL != opages) *opages = (zbx_uint64_t)v.pgswapout; return SYSINFO_RET_OK; }
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; }
static int VM_MEMORY_PUSED(AGENT_RESULT *result) { struct sysinfo info; if (0 != sysinfo(&info)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } if (0 == info.totalram) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero.")); return SYSINFO_RET_FAIL; } SET_DBL_RESULT(result, (info.totalram - info.freeram) / (double)info.totalram * 100); return SYSINFO_RET_OK; }
int SYSTEM_CPU_INTR(AGENT_REQUEST *request, AGENT_RESULT *result) { kstat_ctl_t *kc; kstat_t *k; cpu_stat_t *cpu; int cpu_count = 0; double intr_count = 0.0; if (NULL == (kc = kstat_open())) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open kernel statistics facility: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } k = kc->kc_chain; while (NULL != k) { if (0 == strncmp(k->ks_name, "cpu_stat", 8) && -1 != kstat_read(kc, k, NULL)) { cpu = (cpu_stat_t *)k->ks_data; intr_count += (double)cpu->cpu_sysinfo.intr; cpu_count += 1; } k = k->ks_next; } kstat_close(kc); if (0 == cpu_count) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot find CPU information.")); return SYSINFO_RET_FAIL; } SET_UI64_RESULT(result, intr_count); return SYSINFO_RET_OK; }
static int VM_MEMORY_PAVAILABLE(AGENT_RESULT *result) { struct sysinfo info; AGENT_RESULT result_tmp; zbx_uint64_t available, total; int ret = SYSINFO_RET_FAIL; if (0 != sysinfo(&info)) { SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } init_result(&result_tmp); ret = VM_MEMORY_AVAILABLE(&result_tmp); if (SYSINFO_RET_FAIL == ret) { SET_MSG_RESULT(result, zbx_strdup(NULL, result_tmp.msg)); goto clean; } available = result_tmp.ui64; total = (zbx_uint64_t)info.totalram * info.mem_unit; if (0 == total) { SET_MSG_RESULT(result, zbx_strdup(NULL, "Cannot calculate percentage because total is zero.")); ret = SYSINFO_RET_FAIL; goto clean; } SET_DBL_RESULT(result, available / (double)total * 100); clean: free_result(&result_tmp); return ret; }
/****************************************************************************** * * * Function: * * * * Purpose: * * * * Parameters: * * * * Return value: * * * * Comments: * * * ******************************************************************************/ int MAIN_ZABBIX_ENTRY() { zbx_thread_args_t *thread_args; int thread_num = 0; int status; if (NULL == CONFIG_LOG_FILE || '\0' == *CONFIG_LOG_FILE) { zabbix_open_log(LOG_TYPE_SYSLOG, CONFIG_LOG_LEVEL, NULL); } else { zabbix_open_log(LOG_TYPE_FILE, CONFIG_LOG_LEVEL, CONFIG_LOG_FILE); } zabbix_log(LOG_LEVEL_INFORMATION, "Starting Job Arranger monitor. Job Arranger %s (revision %s).", JOBARG_VERSION, JOBARG_REVISION); /* --- START THREADS --- */ threads_num = 1; threads = (ZBX_THREAD_HANDLE *) zbx_calloc(threads, threads_num, sizeof(ZBX_THREAD_HANDLE)); /* start the executive thread */ thread_args = (zbx_thread_args_t *) zbx_malloc(NULL, sizeof(zbx_thread_args_t)); thread_args->thread_num = thread_num; thread_args->args = NULL; threads[thread_num++] = zbx_thread_start(monitor_thread, thread_args); while (-1 == wait(&status)) { if (EINTR != errno) { zabbix_log(LOG_LEVEL_ERR, "failed to wait on child processes: %s", zbx_strerror(errno)); break; } } THIS_SHOULD_NEVER_HAPPEN; zbx_on_exit(); return SUCCEED; }
/****************************************************************************** * * * Function: get_latest_data * * * * Purpose: Open the latest trap file. If the current file has been rotated, * * process that and then open the latest file. * * * * Return value: SUCCEED - there are new traps to be parsed * * FAIL - there are no new traps or trap file does not exist * * * * Author: Rudolfs Kreicbergs * * * ******************************************************************************/ static int get_latest_data() { struct stat file_buf; if (-1 != trap_fd) /* a trap file is already open */ { if (0 != stat(CONFIG_SNMPTRAP_FILE, &file_buf)) { /* file might have been renamed or deleted, process the current file */ if (ENOENT != errno) { zabbix_log(LOG_LEVEL_CRIT, "cannot stat [%s]: %s", CONFIG_SNMPTRAP_FILE, zbx_strerror(errno)); } read_traps(); close_trap_file(); } else if (file_buf.st_ino != trap_ino || file_buf.st_size < trap_lastsize) { /* file has been rotated, process the current file */ read_traps(); close_trap_file(); } else if (file_buf.st_size == trap_lastsize) { return FAIL; /* no new traps */ } } if (-1 == trap_fd && -1 == open_trap_file()) return FAIL; return SUCCEED; }
static int get_net_stat(const char *if_name, net_stat_t *ns, char **error) { #if defined(HAVE_LIBPERFSTAT) perfstat_id_t ps_id; perfstat_netinterface_t ps_netif; if (NULL == if_name || '\0' == *if_name) { *error = zbx_strdup(NULL, "Network interface name cannot be empty."); return SYSINFO_RET_FAIL; } strscpy(ps_id.name, if_name); if (-1 == perfstat_netinterface(&ps_id, &ps_netif, sizeof(ps_netif), 1)) { *error = zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno)); return SYSINFO_RET_FAIL; } ns->ibytes = (zbx_uint64_t)ps_netif.ibytes; ns->ipackets = (zbx_uint64_t)ps_netif.ipackets; ns->ierr = (zbx_uint64_t)ps_netif.ierrors; ns->obytes = (zbx_uint64_t)ps_netif.obytes; ns->opackets = (zbx_uint64_t)ps_netif.opackets; ns->oerr = (zbx_uint64_t)ps_netif.oerrors; ns->colls = (zbx_uint64_t)ps_netif.collisions; 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 }
static int VM_MEMORY_PROC_MEMINFO(const char *meminfo_entry, AGENT_RESULT *result) { FILE *f; zbx_uint64_t value; int ret = SYSINFO_RET_FAIL; 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 (SUCCEED == byte_value_from_proc_file(f, meminfo_entry, NULL, &value)) { SET_UI64_RESULT(result, value); ret = SYSINFO_RET_OK; } else SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain value from /proc/meminfo.")); zbx_fclose(f); return ret; }
/****************************************************************************** * * * Function: diskstat_shm_init * * * * Purpose: Allocate shared memory for collecting disk statistics * * * ******************************************************************************/ void diskstat_shm_init() { #ifndef _WINDOWS key_t shm_key; size_t shm_size; /* initially allocate memory for collecting statistics for only 1 disk */ shm_size = sizeof(ZBX_DISKDEVICES_DATA); if (-1 == (shm_key = zbx_ftok(CONFIG_FILE, ZBX_IPC_COLLECTOR_DISKSTAT))) { zabbix_log(LOG_LEVEL_CRIT, "cannot create IPC key for disk statistics collector"); exit(EXIT_FAILURE); } if (-1 == (collector->diskstat_shmid = zbx_shmget(shm_key, shm_size))) { zabbix_log(LOG_LEVEL_CRIT, "cannot allocate shared memory for disk statistics collector"); exit(EXIT_FAILURE); } if ((void *)(-1) == (diskdevices = shmat(collector->diskstat_shmid, NULL, 0))) { zabbix_log(LOG_LEVEL_CRIT, "cannot attach shared memory for disk statistics collector: %s", zbx_strerror(errno)); exit(EXIT_FAILURE); } diskdevices->count = 0; diskdevices->max_diskdev = 1; my_diskstat_shmid = collector->diskstat_shmid; zabbix_log(LOG_LEVEL_DEBUG, "diskstat_shm_init() allocated initial shm segment id:%d" " for disk statistics collector", collector->diskstat_shmid); #endif }
int SYSTEM_HOSTNAME(AGENT_REQUEST *request, AGENT_RESULT *result) { char *hostname; long hostbufsize = 0; #ifdef _SC_HOST_NAME_MAX hostbufsize = sysconf(_SC_HOST_NAME_MAX) + 1; #endif if (0 == hostbufsize) hostbufsize = 256; hostname = zbx_malloc(NULL, hostbufsize); if (0 != gethostname(hostname, hostbufsize)) { zbx_free(hostname); SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain system information: %s", zbx_strerror(errno))); return SYSINFO_RET_FAIL; } SET_STR_RESULT(result, hostname); return SYSINFO_RET_OK; }
void print_fatal_info(int sig, siginfo_t *siginfo, void *context) { #ifdef HAVE_SYS_UCONTEXT_H #if defined(REG_EIP) || defined(REG_RIP) ucontext_t *uctx = (ucontext_t *)context; #endif /* look for GET_PC() macro in sigcontextinfo.h files */ /* of glibc if you wish to add more CPU architectures */ # if defined(REG_EIP) /* i386 */ # define ZBX_GET_REG(uctx, reg) (uctx)->uc_mcontext.gregs[reg] # define ZBX_GET_PC(uctx) ZBX_GET_REG(uctx, REG_EIP) # elif defined(REG_RIP) /* x86_64 */ # define ZBX_GET_REG(uctx, reg) (uctx)->uc_mcontext.gregs[reg] # define ZBX_GET_PC(uctx) ZBX_GET_REG(uctx, REG_RIP) # endif #endif /* HAVE_SYS_UCONTEXT_H */ #ifdef HAVE_EXECINFO_H # define ZBX_BACKTRACE_SIZE 60 char **bcktrc_syms; void *bcktrc[ZBX_BACKTRACE_SIZE]; int bcktrc_sz; #endif /* HAVE_EXECINFO_H */ int i; FILE *fd; zabbix_log(LOG_LEVEL_CRIT, "====== Fatal information: ======"); #ifdef HAVE_SYS_UCONTEXT_H #ifdef ZBX_GET_PC zabbix_log(LOG_LEVEL_CRIT, "Program counter: %p", ZBX_GET_PC(uctx)); zabbix_log(LOG_LEVEL_CRIT, "=== Registers: ==="); for (i = 0; i < NGREG; i++) zabbix_log(LOG_LEVEL_CRIT, "%-7s = %16lx = %20lu = %20ld", get_register_name(i), ZBX_GET_REG(uctx, i), ZBX_GET_REG(uctx, i), ZBX_GET_REG(uctx, i)); #ifdef REG_EBP /* dump a bit of stack frame for i386 */ zabbix_log(LOG_LEVEL_CRIT, "=== Stack frame: ==="); for (i = 16; i >= 2; i--) zabbix_log(LOG_LEVEL_CRIT, "+0x%02x(%%ebp) = ebp + %2d = %08x = %10u = %11d%s", i * ZBX_PTR_SIZE, i * ZBX_PTR_SIZE, *(unsigned int *)((void *)ZBX_GET_REG(uctx, REG_EBP) + i * ZBX_PTR_SIZE), *(unsigned int *)((void *)ZBX_GET_REG(uctx, REG_EBP) + i * ZBX_PTR_SIZE), *(unsigned int *)((void *)ZBX_GET_REG(uctx, REG_EBP) + i * ZBX_PTR_SIZE), i == 2 ? " <--- call arguments" : ""); zabbix_log(LOG_LEVEL_CRIT, "+0x%02x(%%ebp) = ebp + %2d = %08x%28s<--- return address", ZBX_PTR_SIZE, ZBX_PTR_SIZE, *(unsigned int *)((void *)ZBX_GET_REG(uctx, REG_EBP) + ZBX_PTR_SIZE), ""); zabbix_log(LOG_LEVEL_CRIT, " (%%ebp) = ebp = %08x%28s<--- saved ebp value", *(unsigned int *)((void *)ZBX_GET_REG(uctx, REG_EBP)), ""); for (i = 1; i <= 16; i++) zabbix_log(LOG_LEVEL_CRIT, "-0x%02x(%%ebp) = ebp - %2d = %08x = %10u = %11d%s", i * ZBX_PTR_SIZE, i * ZBX_PTR_SIZE, *(unsigned int *)((void *)ZBX_GET_REG(uctx, REG_EBP) - i * ZBX_PTR_SIZE), *(unsigned int *)((void *)ZBX_GET_REG(uctx, REG_EBP) - i * ZBX_PTR_SIZE), *(unsigned int *)((void *)ZBX_GET_REG(uctx, REG_EBP) - i * ZBX_PTR_SIZE), i == 1 ? " <--- local variables" : ""); #endif /* REG_EBP */ #else zabbix_log(LOG_LEVEL_CRIT, "program counter not available for this architecture"); zabbix_log(LOG_LEVEL_CRIT, "=== Registers: ==="); zabbix_log(LOG_LEVEL_CRIT, "register dump not available for this architecture"); #endif /* ZBX_GET_PC */ #endif /* HAVE_SYS_UCONTEXT_H */ zabbix_log(LOG_LEVEL_CRIT, "=== Backtrace: ==="); #ifdef HAVE_EXECINFO_H bcktrc_sz = backtrace(bcktrc, ZBX_BACKTRACE_SIZE); bcktrc_syms = backtrace_symbols(bcktrc, bcktrc_sz); if (NULL == bcktrc_syms) { zabbix_log(LOG_LEVEL_CRIT, "error in backtrace_symbols(): %s", zbx_strerror(errno)); for (i = 0; i < bcktrc_sz; i++) zabbix_log(LOG_LEVEL_CRIT, "%d: %p", bcktrc_sz - i - 1, bcktrc[i]); } else { for (i = 0; i < bcktrc_sz; i++) zabbix_log(LOG_LEVEL_CRIT, "%d: %s", bcktrc_sz - i - 1, bcktrc_syms[i]); zbx_free(bcktrc_syms); } #else zabbix_log(LOG_LEVEL_CRIT, "backtrace not available for this platform"); #endif /* HAVE_EXECINFO_H */ zabbix_log(LOG_LEVEL_CRIT, "=== Memory map: ==="); if (NULL != (fd = fopen("/proc/self/maps", "r"))) { char line[1024]; while (NULL != fgets(line, sizeof(line), fd)) { if (line[0] != '\0') line[strlen(line) - 1] = '\0'; /* remove trailing '\n' */ zabbix_log(LOG_LEVEL_CRIT, "%s", line); } zbx_fclose(fd); } else zabbix_log(LOG_LEVEL_CRIT, "memory map not available for this platform"); #ifdef ZBX_GET_PC zabbix_log(LOG_LEVEL_CRIT, "================================"); zabbix_log(LOG_LEVEL_CRIT, "Please consider attaching a disassembly listing to your bug report."); zabbix_log(LOG_LEVEL_CRIT, "This listing can be produced with, e.g., objdump -DSswx %s.", progname); #endif zabbix_log(LOG_LEVEL_CRIT, "================================"); }
/****************************************************************************** * * * Function: diskstat_shm_extend * * * * Purpose: create a new, larger disk statistics shared memory segment and * * copy data from the old one. * * * ******************************************************************************/ void diskstat_shm_extend() { #ifndef _WINDOWS const char *__function_name = "diskstat_shm_extend"; key_t shm_key; size_t old_shm_size, new_shm_size; int old_shmid, new_shmid, old_max, new_max; ZBX_DISKDEVICES_DATA *new_diskdevices; zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name); /* caclulate the size of the new shared memory segment */ old_max = diskdevices->max_diskdev; if (old_max < 4) new_max = old_max + 1; else if (old_max < 256) new_max = old_max * 2; else new_max = old_max + 256; old_shm_size = sizeof(ZBX_DISKDEVICES_DATA) + sizeof(ZBX_SINGLE_DISKDEVICE_DATA) * (old_max - 1); new_shm_size = sizeof(ZBX_DISKDEVICES_DATA) + sizeof(ZBX_SINGLE_DISKDEVICE_DATA) * (new_max - 1); /* Create the new shared memory segment. The same key is used. */ if (-1 == (shm_key = zbx_ftok(CONFIG_FILE, ZBX_IPC_COLLECTOR_DISKSTAT))) { zabbix_log(LOG_LEVEL_CRIT, "cannot create IPC key for extending disk statistics collector"); exit(EXIT_FAILURE); } /* zbx_shmget() will: */ /* - see that a shared memory segment with this key exists */ /* - mark it for deletion */ /* - create a new segment with this key, but with a different id */ if (-1 == (new_shmid = zbx_shmget(shm_key, new_shm_size))) { zabbix_log(LOG_LEVEL_CRIT, "cannot allocate shared memory for extending disk statistics collector"); exit(EXIT_FAILURE); } if ((void *)(-1) == (new_diskdevices = shmat(new_shmid, NULL, 0))) { zabbix_log(LOG_LEVEL_CRIT, "cannot attach shared memory for extending disk statistics collector: %s", zbx_strerror(errno)); exit(EXIT_FAILURE); } /* copy data from the old segment */ memcpy(new_diskdevices, diskdevices, old_shm_size); new_diskdevices->max_diskdev = new_max; /* delete the old segment */ if (-1 == shmdt((void *) diskdevices)) { zabbix_log(LOG_LEVEL_CRIT, "cannot detach from disk statistics collector shared memory"); exit(EXIT_FAILURE); } /* switch to the new segment */ old_shmid = collector->diskstat_shmid; collector->diskstat_shmid = new_shmid; my_diskstat_shmid = collector->diskstat_shmid; diskdevices = new_diskdevices; zabbix_log(LOG_LEVEL_DEBUG, "End of %s() extended diskstat shared memory: old_max:%d new_max:%d old_size:%d" " new_size:%d old_shmid:%d new_shmid:%d", __function_name, old_max, new_max, old_shm_size, new_shm_size, old_shmid, collector->diskstat_shmid); #endif }