Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/plugins/check_nbprocs.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2014,2015 Davide Madrisan <[email protected]>
5 *
6 * A Nagios plugin that displays the number of running processes per user.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 *
21 */
22
23 #include <sys/resource.h>
24 #include <sys/time.h>
25 #include <getopt.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28
29 #include "common.h"
30 #include "messages.h"
31 #include "processes.h"
32 #include "progname.h"
33 #include "progversion.h"
34 #include "thresholds.h"
35
36 static const char *program_copyright =
37 "Copyright (C) 2014,2015 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
38
39 static struct option const longopts[] = {
40 {(char *) "threads", no_argument, NULL, 't'},
41 {(char *) "critical", required_argument, NULL, 'c'},
42 {(char *) "warning", required_argument, NULL, 'w'},
43 {(char *) "verbose", no_argument, NULL, 'v'},
44 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
45 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
46 {NULL, 0, NULL, 0}
47 };
48
49 static _Noreturn void
50 usage (FILE * out)
51 {
52 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
53 fputs ("This plugin displays the number of running processes per user.\n",
54 out);
55 fputs (program_copyright, out);
56 fputs (USAGE_HEADER, out);
57 fprintf (out, " %s [-w COUNTER ] [-c COUNTER]\n",
58 program_name);
59 fputs (USAGE_OPTIONS, out);
60 fputs (" -t, --threads display the number of threads\n", out);
61 fputs (" -w, --warning COUNTER warning threshold\n", out);
62 fputs (" -c, --critical COUNTER critical threshold\n", out);
63 fputs (" -v, --verbose show details for command-line debugging "
64 "(Nagios may truncate output)\n", out);
65 fputs (USAGE_HELP, out);
66 fputs (USAGE_VERSION, out);
67 fputs (USAGE_EXAMPLES, out);
68 fprintf (out, " %s\n", program_name);
69 fprintf (out, " %s --threads -w 1500 -c 2000\n", program_name);
70
71 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
72 }
73
74 static _Noreturn void
75 print_version (void)
76 {
77 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
78 fputs (program_copyright, stdout);
79 fputs (GPLv3_DISCLAIMER, stdout);
80
81 exit (STATE_OK);
82 }
83
84 int
85 main (int argc, char **argv)
86 {
87 int c;
88 unsigned int nbprocs_flags = NBPROCS_NONE;
89 char *critical = NULL, *warning = NULL;
90 nagstatus status = STATE_OK;
91 thresholds *my_threshold = NULL;
92 struct procs_list_node *procs_list, *node;
93
94 set_program_name (argv[0]);
95
96 while ((c = getopt_long (argc, argv,
97 "c:w:v" GETOPT_HELP_VERSION_STRING,
98 longopts, NULL)) != -1)
99 {
100 switch (c)
101 {
102 default:
103 usage (stderr);
104 case 't':
105 nbprocs_flags |= NBPROCS_THREADS;
106 break;
107 case 'c':
108 critical = optarg;
109 break;
110 case 'w':
111 warning = optarg;
112 break;
113 case 'v':
114 nbprocs_flags |= NBPROCS_VERBOSE;
115 break;
116
117 case_GETOPT_HELP_CHAR
118 case_GETOPT_VERSION_CHAR
119
120 }
121 }
122
123 status = set_thresholds (&my_threshold, warning, critical);
124 if (status == NP_RANGE_UNPARSEABLE)
125 usage (stderr);
126
127 procs_list = procs_list_getall (nbprocs_flags);
128
129 status = get_status (procs_list_node_get_total_procs_nbr (procs_list),
130 my_threshold);
131 free (my_threshold);
132
133 printf ("%s %s - %ld running processes | ",
134 program_name_short, state_text (status),
135 procs_list_node_get_total_procs_nbr (procs_list));
136
137 proc_list_node_foreach (node, procs_list)
138 #ifdef RLIMIT_NPROC
139 /* 'label'=value[UOM];[warn];[crit];[min];[max] */
140 printf ("nbr_%s=%ld;%lu;%lu;0 ", procs_list_node_get_username (node),
141 procs_list_node_get_nbr (node),
142 procs_list_node_get_rlimit_nproc_soft (node),
143 procs_list_node_get_rlimit_nproc_hard (node));
144 #else
145 printf ("nbr_%s=%ld ", procs_list_node_get_username (node),
146 procs_list_node_get_nbr (node));
147 #endif
148 putchar ('\n');
149
150 return status;
151 }