check_mrtg.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /******************************************************************************
  2. *
  3. * Nagios check_mrtg plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2006 nagios-plugins team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_mrtg plugin
  13. *
  14. * This plugin will check either the average or maximum value of one of the
  15. * two variables recorded in an MRTG log file.
  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_mrtg";
  36. const char *revision = "$Revision$";
  37. const char *copyright = "1999-2006";
  38. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  39. #include "common.h"
  40. #include "utils.h"
  41. int process_arguments (int, char **);
  42. int validate_arguments (void);
  43. void print_help (void);
  44. void print_usage (void);
  45. char *log_file = NULL;
  46. int expire_minutes = 0;
  47. int use_average = TRUE;
  48. int variable_number = -1;
  49. unsigned long value_warning_threshold = 0L;
  50. unsigned long value_critical_threshold = 0L;
  51. char *label;
  52. char *units;
  53. int
  54. main (int argc, char **argv)
  55. {
  56. int result = STATE_UNKNOWN;
  57. FILE *fp;
  58. int line;
  59. char input_buffer[MAX_INPUT_BUFFER];
  60. char *temp_buffer;
  61. time_t current_time;
  62. time_t timestamp = 0L;
  63. unsigned long average_value_rate = 0L;
  64. unsigned long maximum_value_rate = 0L;
  65. unsigned long rate = 0L;
  66. setlocale (LC_ALL, "");
  67. bindtextdomain (PACKAGE, LOCALEDIR);
  68. textdomain (PACKAGE);
  69. if (process_arguments (argc, argv) == ERROR)
  70. usage4 (_("Could not parse arguments\n"));
  71. /* open the MRTG log file for reading */
  72. fp = fopen (log_file, "r");
  73. if (fp == NULL) {
  74. printf (_("Unable to open MRTG log file\n"));
  75. return STATE_UNKNOWN;
  76. }
  77. line = 0;
  78. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  79. line++;
  80. /* skip the first line of the log file */
  81. if (line == 1)
  82. continue;
  83. /* break out of read loop if we've passed the number of entries we want to read */
  84. if (line > 2)
  85. break;
  86. /* grab the timestamp */
  87. temp_buffer = strtok (input_buffer, " ");
  88. timestamp = strtoul (temp_buffer, NULL, 10);
  89. /* grab the average value 1 rate */
  90. temp_buffer = strtok (NULL, " ");
  91. if (variable_number == 1)
  92. average_value_rate = strtoul (temp_buffer, NULL, 10);
  93. /* grab the average value 2 rate */
  94. temp_buffer = strtok (NULL, " ");
  95. if (variable_number == 2)
  96. average_value_rate = strtoul (temp_buffer, NULL, 10);
  97. /* grab the maximum value 1 rate */
  98. temp_buffer = strtok (NULL, " ");
  99. if (variable_number == 1)
  100. maximum_value_rate = strtoul (temp_buffer, NULL, 10);
  101. /* grab the maximum value 2 rate */
  102. temp_buffer = strtok (NULL, " ");
  103. if (variable_number == 2)
  104. maximum_value_rate = strtoul (temp_buffer, NULL, 10);
  105. }
  106. /* close the log file */
  107. fclose (fp);
  108. /* if we couldn't read enough data, return an unknown error */
  109. if (line <= 2) {
  110. printf (_("Unable to process MRTG log file\n"));
  111. return STATE_UNKNOWN;
  112. }
  113. /* make sure the MRTG data isn't too old */
  114. time (&current_time);
  115. if (expire_minutes > 0
  116. && (current_time - timestamp) > (expire_minutes * 60)) {
  117. printf (_("MRTG data has expired (%d minutes old)\n"),
  118. (int) ((current_time - timestamp) / 60));
  119. return STATE_WARNING;
  120. }
  121. /* else check the incoming/outgoing rates */
  122. if (use_average == TRUE)
  123. rate = average_value_rate;
  124. else
  125. rate = maximum_value_rate;
  126. if (rate > value_critical_threshold)
  127. result = STATE_CRITICAL;
  128. else if (rate > value_warning_threshold)
  129. result = STATE_WARNING;
  130. printf("%s. %s = %lu %s|%s\n",
  131. (use_average == TRUE) ? _("Avg") : _("Max"),
  132. label, rate, units,
  133. perfdata(label, (long) rate, units,
  134. (int) value_warning_threshold, (long) value_warning_threshold,
  135. (int) value_critical_threshold, (long) value_critical_threshold,
  136. 0, 0, 0, 0));
  137. return result;
  138. }
  139. /* process command-line arguments */
  140. int
  141. process_arguments (int argc, char **argv)
  142. {
  143. int c;
  144. int option = 0;
  145. static struct option longopts[] = {
  146. {"logfile", required_argument, 0, 'F'},
  147. {"expires", required_argument, 0, 'e'},
  148. {"aggregation", required_argument, 0, 'a'},
  149. {"variable", required_argument, 0, 'v'},
  150. {"critical", required_argument, 0, 'c'},
  151. {"warning", required_argument, 0, 'w'},
  152. {"label", required_argument, 0, 'l'},
  153. {"units", required_argument, 0, 'u'},
  154. {"verbose", no_argument, 0, 'v'},
  155. {"version", no_argument, 0, 'V'},
  156. {"help", no_argument, 0, 'h'},
  157. {0, 0, 0, 0}
  158. };
  159. if (argc < 2)
  160. return ERROR;
  161. for (c = 1; c < argc; c++) {
  162. if (strcmp ("-to", argv[c]) == 0)
  163. strcpy (argv[c], "-t");
  164. else if (strcmp ("-wt", argv[c]) == 0)
  165. strcpy (argv[c], "-w");
  166. else if (strcmp ("-ct", argv[c]) == 0)
  167. strcpy (argv[c], "-c");
  168. }
  169. while (1) {
  170. c = getopt_long (argc, argv, "hVF:e:a:v:c:w:l:u:", longopts,
  171. &option);
  172. if (c == -1 || c == EOF)
  173. break;
  174. switch (c) {
  175. case 'F': /* input file */
  176. log_file = optarg;
  177. break;
  178. case 'e': /* ups name */
  179. expire_minutes = atoi (optarg);
  180. break;
  181. case 'a': /* port */
  182. if (!strcmp (optarg, "MAX"))
  183. use_average = FALSE;
  184. else
  185. use_average = TRUE;
  186. break;
  187. case 'v':
  188. variable_number = atoi (optarg);
  189. if (variable_number < 1 || variable_number > 2)
  190. usage4 (_("Invalid variable number"));
  191. break;
  192. case 'w': /* critical time threshold */
  193. value_warning_threshold = strtoul (optarg, NULL, 10);
  194. break;
  195. case 'c': /* warning time threshold */
  196. value_critical_threshold = strtoul (optarg, NULL, 10);
  197. break;
  198. case 'l': /* label */
  199. label = optarg;
  200. break;
  201. case 'u': /* timeout */
  202. units = optarg;
  203. break;
  204. case 'V': /* version */
  205. print_revision (progname, revision);
  206. exit (STATE_OK);
  207. case 'h': /* help */
  208. print_help ();
  209. exit (STATE_OK);
  210. case '?': /* help */
  211. usage2 (_("Unknown argument"), optarg);
  212. }
  213. }
  214. c = optind;
  215. if (log_file == NULL && argc > c) {
  216. log_file = argv[c++];
  217. }
  218. if (expire_minutes <= 0 && argc > c) {
  219. if (is_intpos (argv[c]))
  220. expire_minutes = atoi (argv[c++]);
  221. else
  222. die (STATE_UNKNOWN,
  223. _("%s is not a valid expiration time\nUse '%s -h' for additional help\n"),
  224. argv[c], progname);
  225. }
  226. if (argc > c && strcmp (argv[c], "MAX") == 0) {
  227. use_average = FALSE;
  228. c++;
  229. }
  230. else if (argc > c && strcmp (argv[c], "AVG") == 0) {
  231. use_average = TRUE;
  232. c++;
  233. }
  234. if (argc > c && variable_number == -1) {
  235. variable_number = atoi (argv[c++]);
  236. if (variable_number < 1 || variable_number > 2) {
  237. printf ("%s :", argv[c]);
  238. usage (_("Invalid variable number\n"));
  239. }
  240. }
  241. if (argc > c && value_warning_threshold == 0) {
  242. value_warning_threshold = strtoul (argv[c++], NULL, 10);
  243. }
  244. if (argc > c && value_critical_threshold == 0) {
  245. value_critical_threshold = strtoul (argv[c++], NULL, 10);
  246. }
  247. if (argc > c && strlen (label) == 0) {
  248. label = argv[c++];
  249. }
  250. if (argc > c && strlen (units) == 0) {
  251. units = argv[c++];
  252. }
  253. return validate_arguments ();
  254. }
  255. int
  256. validate_arguments (void)
  257. {
  258. if (variable_number == -1)
  259. usage4 (_("You must supply the variable number"));
  260. if (label == NULL)
  261. label = strdup ("value");
  262. if (units == NULL)
  263. units = strdup ("");
  264. return OK;
  265. }
  266. void
  267. print_help (void)
  268. {
  269. print_revision (progname, revision);
  270. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  271. printf (COPYRIGHT, copyright, email);
  272. printf ("%s\n", _("This plugin will check either the average or maximum value of one of the"));
  273. printf ("%s\n", _("two variables recorded in an MRTG log file."));
  274. printf ("\n\n");
  275. print_usage ();
  276. printf (_(UT_HELP_VRSN));
  277. printf (" %s\n", "-F, --logfile=FILE");
  278. printf (" %s\n", _("The MRTG log file containing the data you want to monitor"));
  279. printf ("-e, --expires=MINUTES");
  280. printf (" %s\n", _("Minutes before MRTG data is considered to be too old"));
  281. printf (" %s\n", "-a, --aggregation=AVG|MAX");
  282. printf (" %s\n", _("Should we check average or maximum values?"));
  283. printf (" %s\n", "-v, --variable=INTEGER");
  284. printf (" %s\n", _("Which variable set should we inspect? (1 or 2)"));
  285. printf (" %s\n", "-w, --warning=INTEGER");
  286. printf (" %s\n", _("Threshold value for data to result in WARNING status"));
  287. printf (" %s\n", "-c, --critical=INTEGER");
  288. printf (" %s\n", _("Threshold value for data to result in CRITICAL status"));
  289. printf (" %s\n", "-l, --label=STRING");
  290. printf (" %s\n", _("Type label for data (Examples: Conns, \"Processor Load\", In, Out)"));
  291. printf (" %s\n", "-u, --units=STRING");
  292. printf (" %s\n", _("Option units label for data (Example: Packets/Sec, Errors/Sec,"));
  293. printf (" %s\n", _("\"Bytes Per Second\", \"%% Utilization\")"));
  294. printf ("%s\n", _("If the value exceeds the <vwl> threshold, a WARNING status is returned. If"));
  295. printf ("%s\n", _("the value exceeds the <vcl> threshold, a CRITICAL status is returned. If"));
  296. printf ("%s\n", _("the data in the log file is older than <expire_minutes> old, a WARNING"));
  297. printf ("%s\n", _("status is returned and a warning message is printed."));
  298. printf ("%s\n", _("This plugin is useful for monitoring MRTG data that does not correspond to"));
  299. printf ("%s\n", _("bandwidth usage. (Use the check_mrtgtraf plugin for monitoring bandwidth)."));
  300. printf ("%s\n", _("It can be used to monitor any kind of data that MRTG is monitoring - errors,"));
  301. printf ("%s\n", _("packets/sec, etc. I use MRTG in conjuction with the Novell NLM that allows"));
  302. printf ("%s\n", _("me to track processor utilization, user connections, drive space, etc and"));
  303. printf ("%s\n\n", _("this plugin works well for monitoring that kind of data as well."));
  304. printf ("%s\n", _("Notes:"));
  305. printf (" %s\n", _("- This plugin only monitors one of the two variables stored in the MRTG log"));
  306. printf (" %s\n", _(" file. If you want to monitor both values you will have to define two"));
  307. printf (" %s\n", _(" commands with different values for the <variable> argument. Of course,"));
  308. printf (" %s\n", _("you can always hack the code to make this plugin work for you..."));
  309. printf (" %s\n", _("- MRTG stands for the Multi Router Traffic Grapher. It can be downloaded from"));
  310. printf (" %s\n", _("http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html"));
  311. printf (_(UT_SUPPORT));
  312. }
  313. /* original command line:
  314. <log_file> <expire_minutes> <AVG|MAX> <variable> <vwl> <vcl> <label> [units] */
  315. void
  316. print_usage (void)
  317. {
  318. printf (_("Usage:"));
  319. printf ("%s -F log_file -a <AVG | MAX> -v variable -w warning -c critical\n",progname);
  320. printf ("[-l label] [-u units] [-e expire_minutes] [-t timeout] [-v]\n");
  321. }