check_overcr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*****************************************************************************
  2. *
  3. * Nagios check_overcr plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2007 Nagios Plugins Development Team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_overcr plugin
  13. *
  14. * This plugin attempts to contact the Over-CR collector daemon running on the
  15. * remote UNIX server in order to gather the requested system information.
  16. *
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation, either version 3 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. * $Id$
  32. *
  33. *****************************************************************************/
  34. const char *progname = "check_overcr";
  35. const char *revision = "$Revision$";
  36. const char *copyright = "2000-2007";
  37. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  38. #include "common.h"
  39. #include "netutils.h"
  40. #include "utils.h"
  41. enum checkvar {
  42. NONE,
  43. LOAD1,
  44. LOAD5,
  45. LOAD15,
  46. DPU,
  47. PROCS,
  48. NETSTAT,
  49. UPTIME
  50. };
  51. enum {
  52. PORT = 2000
  53. };
  54. char *server_address = NULL;
  55. int server_port = PORT;
  56. double warning_value = 0L;
  57. double critical_value = 0L;
  58. int check_warning_value = FALSE;
  59. int check_critical_value = FALSE;
  60. enum checkvar vars_to_check = NONE;
  61. int cmd_timeout = 1;
  62. int netstat_port = 0;
  63. char *disk_name = NULL;
  64. char *process_name = NULL;
  65. char send_buffer[MAX_INPUT_BUFFER];
  66. int process_arguments (int, char **);
  67. void print_usage (void);
  68. void print_help (void);
  69. int
  70. main (int argc, char **argv)
  71. {
  72. int result = STATE_UNKNOWN;
  73. char recv_buffer[MAX_INPUT_BUFFER];
  74. char temp_buffer[MAX_INPUT_BUFFER];
  75. char *temp_ptr = NULL;
  76. int found_disk = FALSE;
  77. unsigned long percent_used_disk_space = 100;
  78. double load;
  79. double load_1min;
  80. double load_5min;
  81. double load_15min;
  82. int port_connections = 0;
  83. int processes = 0;
  84. double uptime_raw_hours;
  85. int uptime_raw_minutes = 0;
  86. int uptime_days = 0;
  87. int uptime_hours = 0;
  88. int uptime_minutes = 0;
  89. setlocale (LC_ALL, "");
  90. bindtextdomain (PACKAGE, LOCALEDIR);
  91. textdomain (PACKAGE);
  92. /* Parse extra opts if any */
  93. argv=np_extra_opts (&argc, argv, progname);
  94. if (process_arguments (argc, argv) == ERROR)
  95. usage4 (_("Could not parse arguments"));
  96. /* initialize alarm signal handling */
  97. signal (SIGALRM, socket_timeout_alarm_handler);
  98. /* set socket timeout */
  99. alarm (socket_timeout);
  100. result = process_tcp_request2 (server_address,
  101. server_port,
  102. send_buffer,
  103. recv_buffer,
  104. sizeof (recv_buffer));
  105. switch (vars_to_check) {
  106. case LOAD1:
  107. case LOAD5:
  108. case LOAD15:
  109. if (result != STATE_OK)
  110. die (result, _("Unknown error fetching load data\n"));
  111. temp_ptr = (char *) strtok (recv_buffer, "\r\n");
  112. if (temp_ptr == NULL)
  113. die (STATE_CRITICAL, _("Invalid response from server - no load information\n"));
  114. else
  115. load_1min = strtod (temp_ptr, NULL);
  116. temp_ptr = (char *) strtok (NULL, "\r\n");
  117. if (temp_ptr == NULL)
  118. die (STATE_CRITICAL, _("Invalid response from server after load 1\n"));
  119. else
  120. load_5min = strtod (temp_ptr, NULL);
  121. temp_ptr = (char *) strtok (NULL, "\r\n");
  122. if (temp_ptr == NULL)
  123. die (STATE_CRITICAL, _("Invalid response from server after load 5\n"));
  124. else
  125. load_15min = strtod (temp_ptr, NULL);
  126. switch (vars_to_check) {
  127. case LOAD1:
  128. strcpy (temp_buffer, "1");
  129. load = load_1min;
  130. break;
  131. case LOAD5:
  132. strcpy (temp_buffer, "5");
  133. load = load_5min;
  134. break;
  135. default:
  136. strcpy (temp_buffer, "15");
  137. load = load_15min;
  138. break;
  139. }
  140. if (check_critical_value == TRUE && (load >= critical_value))
  141. result = STATE_CRITICAL;
  142. else if (check_warning_value == TRUE && (load >= warning_value))
  143. result = STATE_WARNING;
  144. die (result,
  145. _("Load %s - %s-min load average = %0.2f"),
  146. state_text(result),
  147. temp_buffer,
  148. load);
  149. break;
  150. case DPU:
  151. if (result != STATE_OK)
  152. die (result, _("Unknown error fetching disk data\n"));
  153. for (temp_ptr = (char *) strtok (recv_buffer, " ");
  154. temp_ptr != NULL;
  155. temp_ptr = (char *) strtok (NULL, " ")) {
  156. if (!strcmp (temp_ptr, disk_name)) {
  157. found_disk = TRUE;
  158. temp_ptr = (char *) strtok (NULL, "%");
  159. if (temp_ptr == NULL)
  160. die (STATE_CRITICAL, _("Invalid response from server\n"));
  161. else
  162. percent_used_disk_space = strtoul (temp_ptr, NULL, 10);
  163. break;
  164. }
  165. temp_ptr = (char *) strtok (NULL, "\r\n");
  166. }
  167. /* error if we couldn't find the info for the disk */
  168. if (found_disk == FALSE)
  169. die (STATE_CRITICAL,
  170. "CRITICAL - Disk '%s' non-existent or not mounted",
  171. disk_name);
  172. if (check_critical_value == TRUE && (percent_used_disk_space >= critical_value))
  173. result = STATE_CRITICAL;
  174. else if (check_warning_value == TRUE && (percent_used_disk_space >= warning_value))
  175. result = STATE_WARNING;
  176. die (result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name);
  177. break;
  178. case NETSTAT:
  179. if (result != STATE_OK)
  180. die (result, _("Unknown error fetching network status\n"));
  181. else
  182. port_connections = strtod (recv_buffer, NULL);
  183. if (check_critical_value == TRUE && (port_connections >= critical_value))
  184. result = STATE_CRITICAL;
  185. else if (check_warning_value == TRUE && (port_connections >= warning_value))
  186. result = STATE_WARNING;
  187. die (result,
  188. _("Net %s - %d connection%s on port %d"),
  189. state_text(result),
  190. port_connections,
  191. (port_connections == 1) ? "" : "s",
  192. netstat_port);
  193. break;
  194. case PROCS:
  195. if (result != STATE_OK)
  196. die (result, _("Unknown error fetching process status\n"));
  197. temp_ptr = (char *) strtok (recv_buffer, "(");
  198. if (temp_ptr == NULL)
  199. die (STATE_CRITICAL, _("Invalid response from server\n"));
  200. temp_ptr = (char *) strtok (NULL, ")");
  201. if (temp_ptr == NULL)
  202. die (STATE_CRITICAL, _("Invalid response from server\n"));
  203. else
  204. processes = strtod (temp_ptr, NULL);
  205. if (check_critical_value == TRUE && (processes >= critical_value))
  206. result = STATE_CRITICAL;
  207. else if (check_warning_value == TRUE && (processes >= warning_value))
  208. result = STATE_WARNING;
  209. die (result,
  210. _("Process %s - %d instance%s of %s running"),
  211. state_text(result),
  212. processes,
  213. (processes == 1) ? "" : "s",
  214. process_name);
  215. break;
  216. case UPTIME:
  217. if (result != STATE_OK)
  218. return result;
  219. uptime_raw_hours = strtod (recv_buffer, NULL);
  220. uptime_raw_minutes = (unsigned long) (uptime_raw_hours * 60.0);
  221. if (check_critical_value == TRUE && (uptime_raw_minutes <= critical_value))
  222. result = STATE_CRITICAL;
  223. else if (check_warning_value == TRUE && (uptime_raw_minutes <= warning_value))
  224. result = STATE_WARNING;
  225. uptime_days = uptime_raw_minutes / 1440;
  226. uptime_raw_minutes %= 1440;
  227. uptime_hours = uptime_raw_minutes / 60;
  228. uptime_raw_minutes %= 60;
  229. uptime_minutes = uptime_raw_minutes;
  230. die (result,
  231. _("Uptime %s - Up %d days %d hours %d minutes"),
  232. state_text(result),
  233. uptime_days,
  234. uptime_hours,
  235. uptime_minutes);
  236. break;
  237. default:
  238. die (STATE_UNKNOWN, _("Nothing to check!\n"));
  239. break;
  240. }
  241. }
  242. /* process command-line arguments */
  243. int
  244. process_arguments (int argc, char **argv)
  245. {
  246. int c;
  247. int option = 0;
  248. static struct option longopts[] = {
  249. {"port", required_argument, 0, 'p'},
  250. {"timeout", required_argument, 0, 't'},
  251. {"critical", required_argument, 0, 'c'},
  252. {"warning", required_argument, 0, 'w'},
  253. {"variable", required_argument, 0, 'v'},
  254. {"hostname", required_argument, 0, 'H'},
  255. {"version", no_argument, 0, 'V'},
  256. {"help", no_argument, 0, 'h'},
  257. {0, 0, 0, 0}
  258. };
  259. /* no options were supplied */
  260. if (argc < 2)
  261. return ERROR;
  262. /* backwards compatibility */
  263. if (!is_option (argv[1])) {
  264. server_address = argv[1];
  265. argv[1] = argv[0];
  266. argv = &argv[1];
  267. argc--;
  268. }
  269. for (c = 1; c < argc; c++) {
  270. if (strcmp ("-to", argv[c]) == 0)
  271. strcpy (argv[c], "-t");
  272. else if (strcmp ("-wv", argv[c]) == 0)
  273. strcpy (argv[c], "-w");
  274. else if (strcmp ("-cv", argv[c]) == 0)
  275. strcpy (argv[c], "-c");
  276. }
  277. while (1) {
  278. c = getopt_long (argc, argv, "+hVH:t:c:w:p:v:", longopts,
  279. &option);
  280. if (c == -1 || c == EOF || c == 1)
  281. break;
  282. switch (c) {
  283. case '?': /* print short usage statement if args not parsable */
  284. usage5 ();
  285. case 'h': /* help */
  286. print_help ();
  287. exit (STATE_OK);
  288. case 'V': /* version */
  289. print_revision (progname, revision);
  290. exit (STATE_OK);
  291. case 'H': /* hostname */
  292. server_address = optarg;
  293. break;
  294. case 'p': /* port */
  295. if (is_intnonneg (optarg))
  296. server_port = atoi (optarg);
  297. else
  298. die (STATE_UNKNOWN,
  299. _("Server port an integer\n"));
  300. break;
  301. case 'v': /* variable */
  302. if (strcmp (optarg, "LOAD") == 0) {
  303. strcpy (send_buffer, "LOAD\r\nQUIT\r\n");
  304. if (strcmp (optarg, "LOAD1") == 0)
  305. vars_to_check = LOAD1;
  306. else if (strcmp (optarg, "LOAD5") == 0)
  307. vars_to_check = LOAD5;
  308. else if (strcmp (optarg, "LOAD15") == 0)
  309. vars_to_check = LOAD15;
  310. }
  311. else if (strcmp (optarg, "UPTIME") == 0) {
  312. vars_to_check = UPTIME;
  313. strcpy (send_buffer, "UPTIME\r\n");
  314. }
  315. else if (strstr (optarg, "PROC") == optarg) {
  316. vars_to_check = PROCS;
  317. process_name = strscpy (process_name, optarg + 4);
  318. sprintf (send_buffer, "PROCESS %s\r\n", process_name);
  319. }
  320. else if (strstr (optarg, "NET") == optarg) {
  321. vars_to_check = NETSTAT;
  322. netstat_port = atoi (optarg + 3);
  323. sprintf (send_buffer, "NETSTAT %d\r\n", netstat_port);
  324. }
  325. else if (strstr (optarg, "DPU") == optarg) {
  326. vars_to_check = DPU;
  327. strcpy (send_buffer, "DISKSPACE\r\n");
  328. disk_name = strscpy (disk_name, optarg + 3);
  329. }
  330. else
  331. return ERROR;
  332. break;
  333. case 'w': /* warning threshold */
  334. warning_value = strtoul (optarg, NULL, 10);
  335. check_warning_value = TRUE;
  336. break;
  337. case 'c': /* critical threshold */
  338. critical_value = strtoul (optarg, NULL, 10);
  339. check_critical_value = TRUE;
  340. break;
  341. case 't': /* timeout */
  342. socket_timeout = atoi (optarg);
  343. if (socket_timeout <= 0)
  344. return ERROR;
  345. }
  346. }
  347. return OK;
  348. }
  349. void
  350. print_help (void)
  351. {
  352. char *myport;
  353. asprintf (&myport, "%d", PORT);
  354. print_revision (progname, revision);
  355. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  356. printf (COPYRIGHT, copyright, email);
  357. printf ("%s\n", _("This plugin attempts to contact the Over-CR collector daemon running on the"));
  358. printf ("%s\n", _("remote UNIX server in order to gather the requested system information."));
  359. printf ("\n\n");
  360. print_usage ();
  361. printf (_(UT_HELP_VRSN));
  362. printf (_(UT_EXTRA_OPTS));
  363. printf (_(UT_HOST_PORT), 'p', myport);
  364. printf (" %s\n", "-w, --warning=INTEGER");
  365. printf (" %s\n", _("Threshold which will result in a warning status"));
  366. printf (" %s\n", "-c, --critical=INTEGER");
  367. printf (" %s\n", _("Threshold which will result in a critical status"));
  368. printf (" %s\n", "-v, --variable=STRING");
  369. printf (" %s\n", _("Variable to check. Valid variables include:"));
  370. printf (" %s\n", _("LOAD1 = 1 minute average CPU load"));
  371. printf (" %s\n", _("LOAD5 = 5 minute average CPU load"));
  372. printf (" %s\n", _("LOAD15 = 15 minute average CPU load"));
  373. printf (" %s\n", _("DPU<filesys> = percent used disk space on filesystem <filesys>"));
  374. printf (" %s\n", _("PROC<process> = number of running processes with name <process>"));
  375. printf (" %s\n", _("NET<port> = number of active connections on TCP port <port>"));
  376. printf (" %s\n", _("UPTIME = system uptime in seconds"));
  377. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  378. printf (_(UT_VERBOSE));
  379. printf ("\n");
  380. printf ("%s\n", _("This plugin requires that Eric Molitors' Over-CR collector daemon be"));
  381. printf ("%s\n", _("running on the remote server."));
  382. printf ("%s\n", _("Over-CR can be downloaded from http://www.molitor.org/overcr"));
  383. printf ("%s\n", _("This plugin was tested with version 0.99.53 of the Over-CR collector"));
  384. printf ("\n");
  385. printf ("%s\n", _("Notes:"));
  386. printf (" %s\n", _("For the available options, the critical threshold value should always be"));
  387. printf (" %s\n", _("higher than the warning threshold value, EXCEPT with the uptime variable"));
  388. #ifdef NP_EXTRA_OPTS
  389. printf ("\n");
  390. printf (_(UT_EXTRA_OPTS_NOTES));
  391. #endif
  392. printf (_(UT_SUPPORT));
  393. }
  394. void
  395. print_usage (void)
  396. {
  397. printf (_("Usage:"));
  398. printf ("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname);
  399. }