Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/plugins/check_tcpcount.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 TCP network and socket informations.
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 <getopt.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "common.h"
28 #include "messages.h"
29 #include "progname.h"
30 #include "progversion.h"
31 #include "tcpinfo.h"
32 #include "thresholds.h"
33
34 static const char *program_copyright =
35 "Copyright (C) 2014,2015 Davide Madrisan <" PACKAGE_BUGREPORT ">\n";
36
37 static struct option const longopts[] = {
38 {(char *) "tcp", no_argument, NULL, 't'},
39 {(char *) "tcp6", no_argument, NULL, '6'},
40 {(char *) "critical", required_argument, NULL, 'c'},
41 {(char *) "warning", required_argument, NULL, 'w'},
42 {(char *) "verbose", no_argument, NULL, 'v'},
43 {(char *) "help", no_argument, NULL, GETOPT_HELP_CHAR},
44 {(char *) "version", no_argument, NULL, GETOPT_VERSION_CHAR},
45 {NULL, 0, NULL, 0}
46 };
47
48 static _Noreturn void
49 usage (FILE * out)
50 {
51 fprintf (out, "%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
52 fputs ("This plugin displays TCP network and socket informations.\n", out);
53 fputs (program_copyright, out);
54 fputs (USAGE_HEADER, out);
55 fprintf (out, " %s [--tcp] [--tcp6] [-w COUNTER] [-c COUNTER]\n",
56 program_name);
57 fputs (USAGE_OPTIONS, out);
58 fputs (" -t, --tcp display the statistics for the TCP protocol "
59 "(the default)\n", out);
60 fputs (" -6, --tcp6 display the statistics for the TCPv6 protocol\n",
61 out);
62 fputs (" -w, --warning COUNTER warning threshold\n", out);
63 fputs (" -c, --critical COUNTER critical threshold\n", out);
64 fputs (" -v, --verbose show details for command-line debugging "
65 "(Nagios may truncate output)\n", out);
66 fputs (USAGE_HELP, out);
67 fputs (USAGE_VERSION, out);
68 fputs (USAGE_EXAMPLES, out);
69 fprintf (out, " %s --tcp -w 1000 -c 1500 # TCPv4 only (the default)\n",
70 program_name);
71 fprintf (out, " %s --tcp --tcp6 -w 1500 -c 2000 # TCPv4 and TCPv6\n",
72 program_name);
73 fprintf (out, " %s --tcp6 -w 1500 -c 2000 # TCPv6 only\n", program_name);
74
75 exit (out == stderr ? STATE_UNKNOWN : STATE_OK);
76 }
77
78 static _Noreturn void
79 print_version (void)
80 {
81 printf ("%s (" PACKAGE_NAME ") v%s\n", program_name, program_version);
82 fputs (program_copyright, stdout);
83 fputs (GPLv3_DISCLAIMER, stdout);
84
85 exit (STATE_OK);
86 }
87
88 int
89 main (int argc, char **argv)
90 {
91 int c, err;
92 bool verbose = false;
93 unsigned int tcp_flags = TCP_UNSET;
94 char *critical = NULL, *warning = NULL;
95 nagstatus status = STATE_OK;
96 thresholds *my_threshold = NULL;
97
98 struct proc_tcptable *tcptable = NULL;
99 unsigned long tcp_established;
100 unsigned long tcp_syn_sent;
101 unsigned long tcp_syn_recv;
102 unsigned long tcp_fin_wait1;
103 unsigned long tcp_fin_wait2;
104 unsigned long tcp_time_wait;
105 unsigned long tcp_close;
106 unsigned long tcp_close_wait;
107 unsigned long tcp_last_ack;
108 unsigned long tcp_listen;
109 unsigned long tcp_closing;
110
111 set_program_name (argv[0]);
112
113 while ((c = getopt_long (argc, argv,
114 "t6c:w:v" GETOPT_HELP_VERSION_STRING,
115 longopts, NULL)) != -1)
116 {
117 switch (c)
118 {
119 default:
120 usage (stderr);
121 case 't':
122 tcp_flags |= TCP_v4;
123 break;
124 case '6':
125 tcp_flags |= TCP_v6;
126 break;
127 case 'c':
128 critical = optarg;
129 break;
130 case 'w':
131 warning = optarg;
132 break;
133 case 'v':
134 verbose = true;
135 break;
136
137 case_GETOPT_HELP_CHAR
138 case_GETOPT_VERSION_CHAR
139
140 }
141 }
142
143 if (tcp_flags == TCP_UNSET)
144 tcp_flags = TCP_v4;
145
146 if (verbose)
147 tcp_flags |= TCP_VERBOSE;
148
149 status = set_thresholds (&my_threshold, warning, critical);
150 if (status == NP_RANGE_UNPARSEABLE)
151 usage (stderr);
152
153 err = proc_tcptable_new (&tcptable);
154 if (err < 0)
155 plugin_error (STATE_UNKNOWN, err, "memory exhausted");
156
157 proc_tcptable_read (tcptable, tcp_flags);
158
159 tcp_established = proc_tcp_get_tcp_established (tcptable);
160 tcp_syn_sent = proc_tcp_get_tcp_syn_sent (tcptable);
161 tcp_syn_recv = proc_tcp_get_tcp_syn_recv (tcptable);
162 tcp_fin_wait1 = proc_tcp_get_tcp_fin_wait1 (tcptable);
163 tcp_fin_wait2 = proc_tcp_get_tcp_fin_wait2 (tcptable);
164 tcp_time_wait = proc_tcp_get_tcp_time_wait (tcptable);
165 tcp_close = proc_tcp_get_tcp_close (tcptable);
166 tcp_close_wait = proc_tcp_get_tcp_close_wait (tcptable);
167 tcp_last_ack = proc_tcp_get_tcp_last_ack (tcptable);
168 tcp_listen = proc_tcp_get_tcp_listen (tcptable);
169 tcp_closing = proc_tcp_get_tcp_closing (tcptable);
170
171 proc_tcptable_unref (tcptable);
172
173 status = get_status (tcp_established, my_threshold);
174 free (my_threshold);
175
176 printf ("%s %s - %lu tcp established | "
177 "tcp_established=%lu tcp_syn_sent=%lu tcp_syn_recv=%lu "
178 "tcp_fin_wait1=%lu tcp_fin_wait2=%lu tcp_time_wait=%lu "
179 "tcp_close=%lu tcp_close_wait=%lu tcp_last_ack=%lu "
180 "tcp_listen=%lu tcp_closing=%lu\n",
181 program_name_short, state_text (status), tcp_established,
182 tcp_established, tcp_syn_sent, tcp_syn_recv,
183 tcp_fin_wait1, tcp_fin_wait2, tcp_time_wait,
184 tcp_close, tcp_close_wait, tcp_last_ack,
185 tcp_listen, tcp_closing);
186
187 return status;
188 }