check_overcr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. if (process_arguments (argc, argv) == ERROR)
  93. usage4 (_("Could not parse arguments"));
  94. /* initialize alarm signal handling */
  95. signal (SIGALRM, socket_timeout_alarm_handler);
  96. /* set socket timeout */
  97. alarm (socket_timeout);
  98. result = process_tcp_request2 (server_address,
  99. server_port,
  100. send_buffer,
  101. recv_buffer,
  102. sizeof (recv_buffer));
  103. switch (vars_to_check) {
  104. case LOAD1:
  105. case LOAD5:
  106. case LOAD15:
  107. if (result != STATE_OK)
  108. die (result, _("Unknown error fetching load data\n"));
  109. temp_ptr = (char *) strtok (recv_buffer, "\r\n");
  110. if (temp_ptr == NULL)
  111. die (STATE_CRITICAL, _("Invalid response from server - no load information\n"));
  112. else
  113. load_1min = strtod (temp_ptr, NULL);
  114. temp_ptr = (char *) strtok (NULL, "\r\n");
  115. if (temp_ptr == NULL)
  116. die (STATE_CRITICAL, _("Invalid response from server after load 1\n"));
  117. else
  118. load_5min = strtod (temp_ptr, NULL);
  119. temp_ptr = (char *) strtok (NULL, "\r\n");
  120. if (temp_ptr == NULL)
  121. die (STATE_CRITICAL, _("Invalid response from server after load 5\n"));
  122. else
  123. load_15min = strtod (temp_ptr, NULL);
  124. switch (vars_to_check) {
  125. case LOAD1:
  126. strcpy (temp_buffer, "1");
  127. load = load_1min;
  128. break;
  129. case LOAD5:
  130. strcpy (temp_buffer, "5");
  131. load = load_5min;
  132. break;
  133. default:
  134. strcpy (temp_buffer, "15");
  135. load = load_15min;
  136. break;
  137. }
  138. if (check_critical_value == TRUE && (load >= critical_value))
  139. result = STATE_CRITICAL;
  140. else if (check_warning_value == TRUE && (load >= warning_value))
  141. result = STATE_WARNING;
  142. die (result,
  143. _("Load %s - %s-min load average = %0.2f"),
  144. state_text(result),
  145. temp_buffer,
  146. load);
  147. break;
  148. case DPU:
  149. if (result != STATE_OK)
  150. die (result, _("Unknown error fetching disk data\n"));
  151. for (temp_ptr = (char *) strtok (recv_buffer, " ");
  152. temp_ptr != NULL;
  153. temp_ptr = (char *) strtok (NULL, " ")) {
  154. if (!strcmp (temp_ptr, disk_name)) {
  155. found_disk = TRUE;
  156. temp_ptr = (char *) strtok (NULL, "%");
  157. if (temp_ptr == NULL)
  158. die (STATE_CRITICAL, _("Invalid response from server\n"));
  159. else
  160. percent_used_disk_space = strtoul (temp_ptr, NULL, 10);
  161. break;
  162. }
  163. temp_ptr = (char *) strtok (NULL, "\r\n");
  164. }
  165. /* error if we couldn't find the info for the disk */
  166. if (found_disk == FALSE)
  167. die (STATE_CRITICAL,
  168. "CRITICAL - Disk '%s' non-existent or not mounted",
  169. disk_name);
  170. if (check_critical_value == TRUE && (percent_used_disk_space >= critical_value))
  171. result = STATE_CRITICAL;
  172. else if (check_warning_value == TRUE && (percent_used_disk_space >= warning_value))
  173. result = STATE_WARNING;
  174. die (result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name);
  175. break;
  176. case NETSTAT:
  177. if (result != STATE_OK)
  178. die (result, _("Unknown error fetching network status\n"));
  179. else
  180. port_connections = strtod (recv_buffer, NULL);
  181. if (check_critical_value == TRUE && (port_connections >= critical_value))
  182. result = STATE_CRITICAL;
  183. else if (check_warning_value == TRUE && (port_connections >= warning_value))
  184. result = STATE_WARNING;
  185. die (result,
  186. _("Net %s - %d connection%s on port %d"),
  187. state_text(result),
  188. port_connections,
  189. (port_connections == 1) ? "" : "s",
  190. netstat_port);
  191. break;
  192. case PROCS:
  193. if (result != STATE_OK)
  194. die (result, _("Unknown error fetching process status\n"));
  195. temp_ptr = (char *) strtok (recv_buffer, "(");
  196. if (temp_ptr == NULL)
  197. die (STATE_CRITICAL, _("Invalid response from server\n"));
  198. temp_ptr = (char *) strtok (NULL, ")");
  199. if (temp_ptr == NULL)
  200. die (STATE_CRITICAL, _("Invalid response from server\n"));
  201. else
  202. processes = strtod (temp_ptr, NULL);
  203. if (check_critical_value == TRUE && (processes >= critical_value))
  204. result = STATE_CRITICAL;
  205. else if (check_warning_value == TRUE && (processes >= warning_value))
  206. result = STATE_WARNING;
  207. die (result,
  208. _("Process %s - %d instance%s of %s running"),
  209. state_text(result),
  210. processes,
  211. (processes == 1) ? "" : "s",
  212. process_name);
  213. break;
  214. case UPTIME:
  215. if (result != STATE_OK)
  216. return result;
  217. uptime_raw_hours = strtod (recv_buffer, NULL);
  218. uptime_raw_minutes = (unsigned long) (uptime_raw_hours * 60.0);
  219. if (check_critical_value == TRUE && (uptime_raw_minutes <= critical_value))
  220. result = STATE_CRITICAL;
  221. else if (check_warning_value == TRUE && (uptime_raw_minutes <= warning_value))
  222. result = STATE_WARNING;
  223. uptime_days = uptime_raw_minutes / 1440;
  224. uptime_raw_minutes %= 1440;
  225. uptime_hours = uptime_raw_minutes / 60;
  226. uptime_raw_minutes %= 60;
  227. uptime_minutes = uptime_raw_minutes;
  228. die (result,
  229. _("Uptime %s - Up %d days %d hours %d minutes"),
  230. state_text(result),
  231. uptime_days,
  232. uptime_hours,
  233. uptime_minutes);
  234. break;
  235. default:
  236. die (STATE_UNKNOWN, _("Nothing to check!\n"));
  237. break;
  238. }
  239. }
  240. /* process command-line arguments */
  241. int
  242. process_arguments (int argc, char **argv)
  243. {
  244. int c;
  245. int option = 0;
  246. static struct option longopts[] = {
  247. {"port", required_argument, 0, 'p'},
  248. {"timeout", required_argument, 0, 't'},
  249. {"critical", required_argument, 0, 'c'},
  250. {"warning", required_argument, 0, 'w'},
  251. {"variable", required_argument, 0, 'v'},
  252. {"hostname", required_argument, 0, 'H'},
  253. {"version", no_argument, 0, 'V'},
  254. {"help", no_argument, 0, 'h'},
  255. {0, 0, 0, 0}
  256. };
  257. /* no options were supplied */
  258. if (argc < 2)
  259. return ERROR;
  260. /* backwards compatibility */
  261. if (!is_option (argv[1])) {
  262. server_address = argv[1];
  263. argv[1] = argv[0];
  264. argv = &argv[1];
  265. argc--;
  266. }
  267. for (c = 1; c < argc; c++) {
  268. if (strcmp ("-to", argv[c]) == 0)
  269. strcpy (argv[c], "-t");
  270. else if (strcmp ("-wv", argv[c]) == 0)
  271. strcpy (argv[c], "-w");
  272. else if (strcmp ("-cv", argv[c]) == 0)
  273. strcpy (argv[c], "-c");
  274. }
  275. while (1) {
  276. c = getopt_long (argc, argv, "+hVH:t:c:w:p:v:", longopts,
  277. &option);
  278. if (c == -1 || c == EOF || c == 1)
  279. break;
  280. switch (c) {
  281. case '?': /* print short usage statement if args not parsable */
  282. usage5 ();
  283. case 'h': /* help */
  284. print_help ();
  285. exit (STATE_OK);
  286. case 'V': /* version */
  287. print_revision (progname, revision);
  288. exit (STATE_OK);
  289. case 'H': /* hostname */
  290. server_address = optarg;
  291. break;
  292. case 'p': /* port */
  293. if (is_intnonneg (optarg))
  294. server_port = atoi (optarg);
  295. else
  296. die (STATE_UNKNOWN,
  297. _("Server port an integer\n"));
  298. break;
  299. case 'v': /* variable */
  300. if (strcmp (optarg, "LOAD") == 0) {
  301. strcpy (send_buffer, "LOAD\r\nQUIT\r\n");
  302. if (strcmp (optarg, "LOAD1") == 0)
  303. vars_to_check = LOAD1;
  304. else if (strcmp (optarg, "LOAD5") == 0)
  305. vars_to_check = LOAD5;
  306. else if (strcmp (optarg, "LOAD15") == 0)
  307. vars_to_check = LOAD15;
  308. }
  309. else if (strcmp (optarg, "UPTIME") == 0) {
  310. vars_to_check = UPTIME;
  311. strcpy (send_buffer, "UPTIME\r\n");
  312. }
  313. else if (strstr (optarg, "PROC") == optarg) {
  314. vars_to_check = PROCS;
  315. process_name = strscpy (process_name, optarg + 4);
  316. sprintf (send_buffer, "PROCESS %s\r\n", process_name);
  317. }
  318. else if (strstr (optarg, "NET") == optarg) {
  319. vars_to_check = NETSTAT;
  320. netstat_port = atoi (optarg + 3);
  321. sprintf (send_buffer, "NETSTAT %d\r\n", netstat_port);
  322. }
  323. else if (strstr (optarg, "DPU") == optarg) {
  324. vars_to_check = DPU;
  325. strcpy (send_buffer, "DISKSPACE\r\n");
  326. disk_name = strscpy (disk_name, optarg + 3);
  327. }
  328. else
  329. return ERROR;
  330. break;
  331. case 'w': /* warning threshold */
  332. warning_value = strtoul (optarg, NULL, 10);
  333. check_warning_value = TRUE;
  334. break;
  335. case 'c': /* critical threshold */
  336. critical_value = strtoul (optarg, NULL, 10);
  337. check_critical_value = TRUE;
  338. break;
  339. case 't': /* timeout */
  340. socket_timeout = atoi (optarg);
  341. if (socket_timeout <= 0)
  342. return ERROR;
  343. }
  344. }
  345. return OK;
  346. }
  347. void
  348. print_help (void)
  349. {
  350. char *myport;
  351. asprintf (&myport, "%d", PORT);
  352. print_revision (progname, revision);
  353. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  354. printf (COPYRIGHT, copyright, email);
  355. printf ("%s\n", _("This plugin attempts to contact the Over-CR collector daemon running on the"));
  356. printf ("%s\n", _("remote UNIX server in order to gather the requested system information."));
  357. printf ("\n\n");
  358. print_usage ();
  359. printf (_(UT_HELP_VRSN));
  360. printf (_(UT_HOST_PORT), 'p', myport);
  361. printf (" %s\n", "-w, --warning=INTEGER");
  362. printf (" %s\n", _("Threshold which will result in a warning status"));
  363. printf (" %s\n", "-c, --critical=INTEGER");
  364. printf (" %s\n", _("Threshold which will result in a critical status"));
  365. printf (" %s\n", "-v, --variable=STRING");
  366. printf (" %s\n", _("Variable to check. Valid variables include:"));
  367. printf (" %s\n", _("LOAD1 = 1 minute average CPU load"));
  368. printf (" %s\n", _("LOAD5 = 5 minute average CPU load"));
  369. printf (" %s\n", _("LOAD15 = 15 minute average CPU load"));
  370. printf (" %s\n", _("DPU<filesys> = percent used disk space on filesystem <filesys>"));
  371. printf (" %s\n", _("PROC<process> = number of running processes with name <process>"));
  372. printf (" %s\n", _("NET<port> = number of active connections on TCP port <port>"));
  373. printf (" %s\n", _("UPTIME = system uptime in seconds"));
  374. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  375. printf (_(UT_VERBOSE));
  376. printf ("\n");
  377. printf ("%s\n", _("Notes:"));
  378. printf ("%s\n", _("For the available options, the critical threshold value should always be"));
  379. printf ("%s\n\n", _("higher than the warning threshold value, EXCEPT with the uptime variable"));
  380. printf ("%s\n", _("This plugin requres 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 (_(UT_SUPPORT));
  385. }
  386. void
  387. print_usage (void)
  388. {
  389. printf (_("Usage:"));
  390. printf ("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname);
  391. }