check_overcr.c 13 KB

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