check_overcr.c 12 KB

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