1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
|
/*
** Zabbix
** Copyright (C) 2001-2019 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include <procfs.h>
#include "common.h"
#include "sysinfo.h"
#include "zbxregexp.h"
#include "log.h"
#include "stats.h"
#if !defined(HAVE_ZONE_H) && defined(HAVE_SYS_UTSNAME_H)
# include <sys/utsname.h>
#endif
extern int CONFIG_TIMEOUT;
typedef struct
{
pid_t pid;
uid_t uid;
char *name;
/* process command line in format <arg0> <arg1> ... <argN>\0 */
char *cmdline;
#ifdef HAVE_ZONE_H
zoneid_t zoneid;
#endif
}
zbx_sysinfo_proc_t;
#ifndef HAVE_ZONE_H
/* helper functions for case if agent is compiled on Solaris 9 or earlier where zones are not supported */
/* but is running on a newer Solaris where zones are supported */
/******************************************************************************
* *
* Function: zbx_solaris_version_get *
* *
* Purpose: get Solaris version at runtime *
* *
* Parameters: *
* major_version - [OUT] major version (e.g. 5) *
* minor_version - [OUT] minor version (e.g. 9 for Solaris 9, 10 for *
* Solaris 10, 11 for Solaris 11) *
* Return value: *
* SUCCEED - no errors, FAIL - an error occurred *
* *
******************************************************************************/
static int zbx_solaris_version_get(unsigned int *major_version, unsigned int *minor_version)
{
const char *__function_name = "zbx_solaris_version_get";
int res;
struct utsname name;
if (-1 == (res = uname(&name)))
{
zabbix_log(LOG_LEVEL_WARNING, "%s(): uname() failed: %s", __function_name, zbx_strerror(errno));
return FAIL;
}
/* expected result in name.release: "5.9" - Solaris 9, "5.10" - Solaris 10, "5.11" - Solaris 11 */
if (2 != sscanf(name.release, "%u.%u", major_version, minor_version))
{
zabbix_log(LOG_LEVEL_WARNING, "%s(): sscanf() failed on: \"%s\"", __function_name, name.release);
THIS_SHOULD_NEVER_HAPPEN;
return FAIL;
}
return SUCCEED;
}
/******************************************************************************
* *
* Function: zbx_detect_zone_support *
* *
* Purpose: find if zones are supported *
* *
* Return value: *
* SUCCEED - zones supported *
* FAIL - zones not supported or error occurred. For our purposes error *
* counts as no support for zones. *
* *
******************************************************************************/
static int zbx_detect_zone_support(void)
{
#define ZBX_ZONE_SUPPORT_UNKNOWN 0
#define ZBX_ZONE_SUPPORT_YES 1
#define ZBX_ZONE_SUPPORT_NO 2
static int zone_support = ZBX_ZONE_SUPPORT_UNKNOWN;
unsigned int major, minor;
switch (zone_support)
{
case ZBX_ZONE_SUPPORT_NO:
return FAIL;
case ZBX_ZONE_SUPPORT_YES:
return SUCCEED;
default:
/* zones are supported in Solaris 10 and later (minimum version is "5.10") */
if (SUCCEED == zbx_solaris_version_get(&major, &minor) &&
((5 == major && 10 <= minor) || 5 < major))
{
zone_support = ZBX_ZONE_SUPPORT_YES;
return SUCCEED;
}
else /* failure to get Solaris version also results in "zones not supported" */
{
zone_support = ZBX_ZONE_SUPPORT_NO;
return FAIL;
}
}
}
#endif
/******************************************************************************
* *
* Function: zbx_sysinfo_proc_free *
* *
* Purpose: frees process data structure *
* *
******************************************************************************/
static void zbx_sysinfo_proc_free(zbx_sysinfo_proc_t *proc)
{
zbx_free(proc->name);
zbx_free(proc->cmdline);
zbx_free(proc);
}
static int check_procstate(psinfo_t *psinfo, int zbx_proc_stat)
{
if (zbx_proc_stat == ZBX_PROC_STAT_ALL)
return SUCCEED;
switch (zbx_proc_stat)
{
case ZBX_PROC_STAT_RUN:
return (psinfo->pr_lwp.pr_state == SRUN || psinfo->pr_lwp.pr_state == SONPROC) ? SUCCEED : FAIL;
case ZBX_PROC_STAT_SLEEP:
return (psinfo->pr_lwp.pr_state == SSLEEP) ? SUCCEED : FAIL;
case ZBX_PROC_STAT_ZOMB:
return (psinfo->pr_lwp.pr_state == SZOMB) ? SUCCEED : FAIL;
}
return FAIL;
}
int PROC_MEM(AGENT_REQUEST *request, AGENT_RESULT *result)
{
char tmp[MAX_STRING_LEN], *procname, *proccomm, *param, *memtype = NULL;
DIR *dir;
struct dirent *entries;
struct passwd *usrinfo;
psinfo_t psinfo; /* In the correct procfs.h, the structure name is psinfo_t */
int fd = -1, do_task, proccount = 0, invalid_user = 0;
zbx_uint64_t mem_size = 0, byte_value = 0;
double pct_size = 0.0, pct_value = 0.0;
size_t *p_value;
if (5 < request->nparam)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
return SYSINFO_RET_FAIL;
}
procname = get_rparam(request, 0);
param = get_rparam(request, 1);
if (NULL != param && '\0' != *param)
{
errno = 0;
if (NULL == (usrinfo = getpwnam(param)))
{
if (0 != errno)
{
SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain user information: %s",
zbx_strerror(errno)));
return SYSINFO_RET_FAIL;
}
invalid_user = 1;
}
}
else
usrinfo = NULL;
param = get_rparam(request, 2);
if (NULL == param || '\0' == *param || 0 == strcmp(param, "sum"))
do_task = ZBX_DO_SUM;
else if (0 == strcmp(param, "avg"))
do_task = ZBX_DO_AVG;
else if (0 == strcmp(param, "max"))
do_task = ZBX_DO_MAX;
else if (0 == strcmp(param, "min"))
do_task = ZBX_DO_MIN;
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
return SYSINFO_RET_FAIL;
}
proccomm = get_rparam(request, 3);
memtype = get_rparam(request, 4);
if (NULL == memtype || '\0' == *memtype || 0 == strcmp(memtype, "vsize"))
{
p_value = &psinfo.pr_size; /* size of process image in Kbytes */
}
else if (0 == strcmp(memtype, "rss"))
{
p_value = &psinfo.pr_rssize; /* resident set size in Kbytes */
}
else if (0 == strcmp(memtype, "pmem"))
{
p_value = NULL; /* for % of system memory used by process */
}
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fifth parameter."));
return SYSINFO_RET_FAIL;
}
if (1 == invalid_user) /* handle 0 for non-existent user after all parameters have been parsed and validated */
goto out;
if (NULL == (dir = opendir("/proc")))
{
SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc: %s", zbx_strerror(errno)));
return SYSINFO_RET_FAIL;
}
while (NULL != (entries = readdir(dir)))
{
if (-1 != fd)
{
close(fd);
fd = -1;
}
zbx_snprintf(tmp, sizeof(tmp), "/proc/%s/psinfo", entries->d_name);
if (-1 == (fd = open(tmp, O_RDONLY)))
continue;
if (-1 == read(fd, &psinfo, sizeof(psinfo)))
continue;
if (NULL != procname && '\0' != *procname && 0 != strcmp(procname, psinfo.pr_fname))
continue;
if (NULL != usrinfo && usrinfo->pw_uid != psinfo.pr_uid)
continue;
if (NULL != proccomm && '\0' != *proccomm && NULL == zbx_regexp_match(psinfo.pr_psargs, proccomm, NULL))
continue;
if (NULL != p_value)
{
/* pr_size or pr_rssize in Kbytes */
byte_value = *p_value << 10; /* kB to Byte */
if (0 != proccount++)
{
if (ZBX_DO_MAX == do_task)
mem_size = MAX(mem_size, byte_value);
else if (ZBX_DO_MIN == do_task)
mem_size = MIN(mem_size, byte_value);
else
mem_size += byte_value;
}
else
mem_size = byte_value;
}
else
{
/* % of system memory used by process, measured in 16-bit binary fractions in the range */
/* 0.0 - 1.0 with the binary point to the right of the most significant bit. 1.0 == 0x8000 */
pct_value = (double)((int)psinfo.pr_pctmem * 100) / 32768.0;
if (0 != proccount++)
{
if (ZBX_DO_MAX == do_task)
pct_size = MAX(pct_size, pct_value);
else if (ZBX_DO_MIN == do_task)
pct_size = MIN(pct_size, pct_value);
else
pct_size += pct_value;
}
else
pct_size = pct_value;
}
}
closedir(dir);
if (-1 != fd)
close(fd);
out:
if (NULL != p_value)
{
if (ZBX_DO_AVG == do_task)
SET_DBL_RESULT(result, 0 == proccount ? 0.0 : (double)mem_size / (double)proccount);
else
SET_UI64_RESULT(result, mem_size);
}
else
{
if (ZBX_DO_AVG == do_task)
SET_DBL_RESULT(result, 0 == proccount ? 0.0 : pct_size / (double)proccount);
else
SET_DBL_RESULT(result, pct_size);
}
return SYSINFO_RET_OK;
}
int PROC_NUM(AGENT_REQUEST *request, AGENT_RESULT *result)
{
char tmp[MAX_STRING_LEN], *procname, *proccomm, *param, *zone_parameter;
DIR *dir;
struct dirent *entries;
zbx_stat_t buf;
struct passwd *usrinfo;
psinfo_t psinfo; /* In the correct procfs.h, the structure name is psinfo_t */
int fd = -1, proccount = 0, invalid_user = 0, zbx_proc_stat;
#ifdef HAVE_ZONE_H
zoneid_t zoneid;
int zoneflag;
#endif
if (5 < request->nparam)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
return SYSINFO_RET_FAIL;
}
procname = get_rparam(request, 0);
param = get_rparam(request, 1);
if (NULL != param && '\0' != *param)
{
errno = 0;
if (NULL == (usrinfo = getpwnam(param)))
{
if (0 != errno)
{
SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot obtain user information: %s",
zbx_strerror(errno)));
return SYSINFO_RET_FAIL;
}
invalid_user = 1;
}
}
else
usrinfo = NULL;
param = get_rparam(request, 2);
if (NULL == param || '\0' == *param || 0 == strcmp(param, "all"))
zbx_proc_stat = ZBX_PROC_STAT_ALL;
else if (0 == strcmp(param, "run"))
zbx_proc_stat = ZBX_PROC_STAT_RUN;
else if (0 == strcmp(param, "sleep"))
zbx_proc_stat = ZBX_PROC_STAT_SLEEP;
else if (0 == strcmp(param, "zomb"))
zbx_proc_stat = ZBX_PROC_STAT_ZOMB;
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
return SYSINFO_RET_FAIL;
}
proccomm = get_rparam(request, 3);
if (NULL == (zone_parameter = get_rparam(request, 4)) || '\0' == *zone_parameter
|| 0 == strcmp(zone_parameter, "current"))
{
#ifdef HAVE_ZONE_H
zoneflag = ZBX_PROCSTAT_FLAGS_ZONE_CURRENT;
#else
if (SUCCEED == zbx_detect_zone_support())
{
/* Agent has been compiled on Solaris 9 or earlier where zones are not supported */
/* but now it is running on a system with zone support. This agent cannot limit */
/* results to only current zone. */
SET_MSG_RESULT(result, zbx_strdup(NULL, "The fifth parameter value \"current\" cannot be used"
" with agent running on a Solaris version with zone support, but compiled on"
" a Solaris version without zone support. Consider using \"all\" or install"
" agent with Solaris zone support."));
return SYSINFO_RET_FAIL;
}
#endif
}
else if (0 == strcmp(zone_parameter, "all"))
{
#ifdef HAVE_ZONE_H
zoneflag = ZBX_PROCSTAT_FLAGS_ZONE_ALL;
#endif
}
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fifth parameter."));
return SYSINFO_RET_FAIL;
}
#ifdef HAVE_ZONE_H
zoneid = getzoneid();
#endif
if (1 == invalid_user) /* handle 0 for non-existent user after all parameters have been parsed and validated */
goto out;
if (NULL == (dir = opendir("/proc")))
{
SET_MSG_RESULT(result, zbx_dsprintf(NULL, "Cannot open /proc: %s", zbx_strerror(errno)));
return SYSINFO_RET_FAIL;
}
while (NULL != (entries = readdir(dir)))
{
if (-1 != fd)
{
close(fd);
fd = -1;
}
zbx_snprintf(tmp, sizeof(tmp), "/proc/%s/psinfo", entries->d_name);
if (0 != zbx_stat(tmp, &buf))
continue;
if (-1 == (fd = open(tmp, O_RDONLY)))
continue;
if (-1 == read(fd, &psinfo, sizeof(psinfo)))
continue;
if (NULL != procname && '\0' != *procname && 0 != strcmp(procname, psinfo.pr_fname))
continue;
if (NULL != usrinfo && usrinfo->pw_uid != psinfo.pr_uid)
continue;
if (FAIL == check_procstate(&psinfo, zbx_proc_stat))
continue;
if (NULL != proccomm && '\0' != *proccomm && NULL == zbx_regexp_match(psinfo.pr_psargs, proccomm, NULL))
continue;
#ifdef HAVE_ZONE_H
if (ZBX_PROCSTAT_FLAGS_ZONE_CURRENT == zoneflag && zoneid != psinfo.pr_zoneid)
continue;
#endif
proccount++;
}
closedir(dir);
if (-1 != fd)
close(fd);
out:
SET_UI64_RESULT(result, proccount);
return SYSINFO_RET_OK;
}
/******************************************************************************
* *
* Function: proc_match_name *
* *
* Purpose: checks if the process name matches filter *
* *
******************************************************************************/
static int proc_match_name(const zbx_sysinfo_proc_t *proc, const char *procname)
{
if (NULL == procname)
return SUCCEED;
if (NULL != proc->name && 0 == strcmp(procname, proc->name))
return SUCCEED;
return FAIL;
}
/******************************************************************************
* *
* Function: proc_match_user *
* *
* Purpose: checks if the process user matches filter *
* *
******************************************************************************/
static int proc_match_user(const zbx_sysinfo_proc_t *proc, const struct passwd *usrinfo)
{
if (NULL == usrinfo)
return SUCCEED;
if (proc->uid == usrinfo->pw_uid)
return SUCCEED;
return FAIL;
}
/******************************************************************************
* *
* Function: proc_match_cmdline *
* *
* Purpose: checks if the process command line matches filter *
* *
******************************************************************************/
static int proc_match_cmdline(const zbx_sysinfo_proc_t *proc, const char *cmdline)
{
if (NULL == cmdline)
return SUCCEED;
if (NULL != proc->cmdline && NULL != zbx_regexp_match(proc->cmdline, cmdline, NULL))
return SUCCEED;
return FAIL;
}
#ifdef HAVE_ZONE_H
/******************************************************************************
* *
* Function: proc_match_zone *
* *
* Purpose: checks if the process zone matches filter *
* *
******************************************************************************/
static int proc_match_zone(const zbx_sysinfo_proc_t *proc, zbx_uint64_t flags, zoneid_t zoneid)
{
if (0 != (ZBX_PROCSTAT_FLAGS_ZONE_ALL & flags))
return SUCCEED;
if (proc->zoneid == zoneid)
return SUCCEED;
return FAIL;
}
#endif
/******************************************************************************
* *
* Function: proc_read_cpu_util *
* *
* Purpose: reads process cpu utilization values from /proc/[pid]/usage file *
* *
* Parameters: procutil - [IN/OUT] the process cpu utilization data *
* *
* Return value: SUCCEED - the process cpu utilization data was read *
* successfully *
* <0 - otherwise, -errno code is returned *
* *
* Comments: we use /proc/[pid]/usage since /proc/[pid]/status contains *
* sensitive information and by default can only be read by the *
* owner or privileged user. *
* *
* In addition to user and system-call CPU time the *
* /proc/[pid]/usage also contains CPU time spent in trap context *
* Currently trap CPU time is not taken into account. *
* *
* prstat(1) skips processes 0 (sched), 2 (pageout) and 3 (fsflush) *
* however we take them into account. *
* *
******************************************************************************/
static int proc_read_cpu_util(zbx_procstat_util_t *procutil)
{
int fd, n;
char tmp[MAX_STRING_LEN];
psinfo_t psinfo;
prusage_t prusage;
zbx_snprintf(tmp, sizeof(tmp), "/proc/%d/psinfo", (int)procutil->pid);
if (-1 == (fd = open(tmp, O_RDONLY)))
return -errno;
n = read(fd, &psinfo, sizeof(psinfo));
close(fd);
if (-1 == n)
return -errno;
procutil->starttime = psinfo.pr_start.tv_sec;
zbx_snprintf(tmp, sizeof(tmp), "/proc/%d/usage", (int)procutil->pid);
if (-1 == (fd = open(tmp, O_RDONLY)))
return -errno;
n = read(fd, &prusage, sizeof(prusage));
close(fd);
if (-1 == n)
return -errno;
/* convert cpu utilization time to clock ticks */
procutil->utime = ((zbx_uint64_t)prusage.pr_utime.tv_sec * 1e9 + prusage.pr_utime.tv_nsec) *
sysconf(_SC_CLK_TCK) / 1e9;
procutil->stime = ((zbx_uint64_t)prusage.pr_stime.tv_sec * 1e9 + prusage.pr_stime.tv_nsec) *
sysconf(_SC_CLK_TCK) / 1e9;
return SUCCEED;
}
/******************************************************************************
* *
* Function: zbx_proc_get_process_stats *
* *
* Purpose: get process cpu utilization data *
* *
* Parameters: procs - [IN/OUT] an array of process utilization data *
* procs_num - [IN] the number of items in procs array *
* *
******************************************************************************/
void zbx_proc_get_process_stats(zbx_procstat_util_t *procs, int procs_num)
{
const char *__function_name = "zbx_proc_get_process_stats";
int i;
zabbix_log(LOG_LEVEL_TRACE, "In %s() procs_num:%d", __function_name, procs_num);
for (i = 0; i < procs_num; i++)
procs[i].error = proc_read_cpu_util(&procs[i]);
zabbix_log(LOG_LEVEL_TRACE, "End of %s()", __function_name);
}
/******************************************************************************
* *
* Function: zbx_proc_get_processes *
* *
* Purpose: get system processes *
* *
* Parameters: processes - [OUT] the system processes *
* flags - [IN] the flags specifying the process properties *
* that must be returned *
* *
* Return value: SUCCEED - the system processes were retrieved successfully *
* FAIL - failed to open /proc directory *
* *
******************************************************************************/
int zbx_proc_get_processes(zbx_vector_ptr_t *processes, unsigned int flags)
{
const char *__function_name = "zbx_proc_get_processes";
DIR *dir;
struct dirent *entries;
char tmp[MAX_STRING_LEN];
int pid, ret = FAIL, fd = -1, n;
psinfo_t psinfo; /* In the correct procfs.h, the structure name is psinfo_t */
zbx_sysinfo_proc_t *proc;
zabbix_log(LOG_LEVEL_TRACE, "In %s()", __function_name);
if (NULL == (dir = opendir("/proc")))
goto out;
while (NULL != (entries = readdir(dir)))
{
/* skip entries not containing pids */
if (FAIL == is_uint32(entries->d_name, &pid))
continue;
zbx_snprintf(tmp, sizeof(tmp), "/proc/%s/psinfo", entries->d_name);
if (-1 == (fd = open(tmp, O_RDONLY)))
continue;
n = read(fd, &psinfo, sizeof(psinfo));
close(fd);
if (-1 == n)
continue;
proc = (zbx_sysinfo_proc_t *)zbx_malloc(NULL, sizeof(zbx_sysinfo_proc_t));
memset(proc, 0, sizeof(zbx_sysinfo_proc_t));
proc->pid = pid;
if (0 != (flags & ZBX_SYSINFO_PROC_NAME))
proc->name = zbx_strdup(NULL, psinfo.pr_fname);
if (0 != (flags & ZBX_SYSINFO_PROC_USER))
proc->uid = psinfo.pr_uid;
if (0 != (flags & ZBX_SYSINFO_PROC_CMDLINE))
proc->cmdline = zbx_strdup(NULL, psinfo.pr_psargs);
#ifdef HAVE_ZONE_H
proc->zoneid = psinfo.pr_zoneid;
#endif
zbx_vector_ptr_append(processes, proc);
}
closedir(dir);
ret = SUCCEED;
out:
zabbix_log(LOG_LEVEL_TRACE, "End of %s(): %s", __function_name, zbx_result_string(ret));
return ret;
}
/******************************************************************************
* *
* Function: zbx_proc_free_processes *
* *
* Purpose: frees process vector read by zbx_proc_get_processes function *
* *
* Parameters: processes - [IN/OUT] the process vector to free *
* *
******************************************************************************/
void zbx_proc_free_processes(zbx_vector_ptr_t *processes)
{
zbx_vector_ptr_clear_ext(processes, (zbx_mem_free_func_t)zbx_sysinfo_proc_free);
}
/******************************************************************************
* *
* Function: zbx_proc_get_matching_pids *
* *
* Purpose: get pids matching the specified process name, user name and *
* command line *
* *
* Parameters: procname - [IN] the process name, NULL - all *
* username - [IN] the user name, NULL - all *
* cmdline - [IN] the command line, NULL - all *
* pids - [OUT] the vector of matching pids *
* *
* Return value: SUCCEED - the pids were read successfully *
* -errno - failed to read pids *
* *
******************************************************************************/
void zbx_proc_get_matching_pids(const zbx_vector_ptr_t *processes, const char *procname, const char *username,
const char *cmdline, zbx_uint64_t flags, zbx_vector_uint64_t *pids)
{
const char *__function_name = "zbx_proc_get_matching_pids";
struct passwd *usrinfo;
int i;
zbx_sysinfo_proc_t *proc;
#ifdef HAVE_ZONE_H
zoneid_t zoneid;
#endif
zabbix_log(LOG_LEVEL_TRACE, "In %s() procname:%s username:%s cmdline:%s zone:%d", __function_name,
ZBX_NULL2EMPTY_STR(procname), ZBX_NULL2EMPTY_STR(username), ZBX_NULL2EMPTY_STR(cmdline), flags);
if (NULL != username)
{
/* in the case of invalid user there are no matching processes, return empty vector */
if (NULL == (usrinfo = getpwnam(username)))
goto out;
}
else
usrinfo = NULL;
#ifdef HAVE_ZONE_H
zoneid = getzoneid();
#endif
for (i = 0; i < processes->values_num; i++)
{
proc = (zbx_sysinfo_proc_t *)processes->values[i];
if (SUCCEED != proc_match_user(proc, usrinfo))
continue;
if (SUCCEED != proc_match_name(proc, procname))
continue;
if (SUCCEED != proc_match_cmdline(proc, cmdline))
continue;
#ifdef HAVE_ZONE_H
if (SUCCEED != proc_match_zone(proc, flags, zoneid))
continue;
#endif
zbx_vector_uint64_append(pids, (zbx_uint64_t)proc->pid);
}
out:
zabbix_log(LOG_LEVEL_TRACE, "End of %s()", __function_name);
}
int PROC_CPU_UTIL(AGENT_REQUEST *request, AGENT_RESULT *result)
{
const char *procname, *username, *cmdline, *tmp, *flags;
char *errmsg = NULL;
int period, type;
double value;
zbx_uint64_t zoneflag;
zbx_timespec_t ts_timeout, ts;
/* proc.cpu.util[<procname>,<username>,(user|system),<cmdline>,(avg1|avg5|avg15),(current|all)] */
if (6 < request->nparam)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Too many parameters."));
return SYSINFO_RET_FAIL;
}
/* zbx_procstat_get_* functions expect NULL for default values - */
/* convert empty procname, username and cmdline strings to NULL values */
if (NULL != (procname = get_rparam(request, 0)) && '\0' == *procname)
procname = NULL;
if (NULL != (username = get_rparam(request, 1)) && '\0' == *username)
username = NULL;
if (NULL != (cmdline = get_rparam(request, 3)) && '\0' == *cmdline)
cmdline = NULL;
/* utilization type parameter (user|system) */
if (NULL == (tmp = get_rparam(request, 2)) || '\0' == *tmp || 0 == strcmp(tmp, "total"))
{
type = ZBX_PROCSTAT_CPU_TOTAL;
}
else if (0 == strcmp(tmp, "user"))
{
type = ZBX_PROCSTAT_CPU_USER;
}
else if (0 == strcmp(tmp, "system"))
{
type = ZBX_PROCSTAT_CPU_SYSTEM;
}
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
return SYSINFO_RET_FAIL;
}
/* mode parameter (avg1|avg5|avg15) */
if (NULL == (tmp = get_rparam(request, 4)) || '\0' == *tmp || 0 == strcmp(tmp, "avg1"))
{
period = SEC_PER_MIN;
}
else if (0 == strcmp(tmp, "avg5"))
{
period = SEC_PER_MIN * 5;
}
else if (0 == strcmp(tmp, "avg15"))
{
period = SEC_PER_MIN * 15;
}
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid fifth parameter."));
return SYSINFO_RET_FAIL;
}
if (NULL == (flags = get_rparam(request, 5)) || '\0' == *flags || 0 == strcmp(flags, "current"))
{
#ifndef HAVE_ZONE_H
if (SUCCEED == zbx_detect_zone_support())
{
/* Agent has been compiled on Solaris 9 or earlier where zones are not supported */
/* but now it is running on a system with zone support. This agent cannot limit */
/* results to only current zone. */
SET_MSG_RESULT(result, zbx_strdup(NULL, "The sixth parameter value \"current\" cannot be used"
" with agent running on a Solaris version with zone support, but compiled on"
" a Solaris version without zone support. Consider using \"all\" or install"
" agent with Solaris zone support."));
return SYSINFO_RET_FAIL;
}
/* zones are not supported, the agent can accept 6th parameter with default value "current" */
#endif
zoneflag = ZBX_PROCSTAT_FLAGS_ZONE_CURRENT;
}
else if (0 == strcmp(flags, "all"))
{
zoneflag = ZBX_PROCSTAT_FLAGS_ZONE_ALL;
}
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid sixth parameter."));
return SYSINFO_RET_FAIL;
}
if (SUCCEED != zbx_procstat_collector_started())
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Collector is not started."));
return SYSINFO_RET_FAIL;
}
zbx_timespec(&ts_timeout);
ts_timeout.sec += CONFIG_TIMEOUT;
while (SUCCEED != zbx_procstat_get_util(procname, username, cmdline, zoneflag, period, type, &value, &errmsg))
{
/* zbx_procstat_get_* functions will return FAIL when either a collection */
/* error was registered or if less than 2 data samples were collected. */
/* In the first case the errmsg will contain error message. */
if (NULL != errmsg)
{
SET_MSG_RESULT(result, errmsg);
return SYSINFO_RET_FAIL;
}
zbx_timespec(&ts);
if (0 > zbx_timespec_compare(&ts_timeout, &ts))
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Timeout while waiting for collector data."));
return SYSINFO_RET_FAIL;
}
sleep(1);
}
SET_DBL_RESULT(result, value);
return SYSINFO_RET_OK;
}
|