check_overcr.c 12 KB

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