check_overcr.c 12 KB

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