check_mrtgtraf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /******************************************************************************
  2. *
  3. * Nagios check_mrtgtraf plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2007 nagios-plugins team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_mtrgtraf plugin
  13. *
  14. * This plugin will check the incoming/outgoing transfer rates of a router
  15. * switch, etc recorded in an MRTG log.
  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. #include "common.h"
  36. #include "utils.h"
  37. const char *progname = "check_mrtgtraf";
  38. const char *revision = "$Revision$";
  39. const char *copyright = "1999-2007";
  40. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  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 = -1;
  47. int use_average = TRUE;
  48. unsigned long incoming_warning_threshold = 0L;
  49. unsigned long incoming_critical_threshold = 0L;
  50. unsigned long outgoing_warning_threshold = 0L;
  51. unsigned long outgoing_critical_threshold = 0L;
  52. int
  53. main (int argc, char **argv)
  54. {
  55. int result = STATE_OK;
  56. FILE *fp;
  57. int line;
  58. char input_buffer[MAX_INPUT_BUFFER];
  59. char *temp_buffer;
  60. time_t current_time;
  61. char *error_message;
  62. time_t timestamp = 0L;
  63. unsigned long average_incoming_rate = 0L;
  64. unsigned long average_outgoing_rate = 0L;
  65. unsigned long maximum_incoming_rate = 0L;
  66. unsigned long maximum_outgoing_rate = 0L;
  67. unsigned long incoming_rate = 0L;
  68. unsigned long outgoing_rate = 0L;
  69. double adjusted_incoming_rate = 0.0;
  70. double adjusted_outgoing_rate = 0.0;
  71. char incoming_speed_rating[8];
  72. char outgoing_speed_rating[8];
  73. if (process_arguments (argc, argv) == ERROR)
  74. usage4 (_("Could not parse arguments"));
  75. /* open the MRTG log file for reading */
  76. fp = fopen (log_file, "r");
  77. if (fp == NULL)
  78. usage4 (_("Unable to open MRTG log file"));
  79. line = 0;
  80. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
  81. line++;
  82. /* skip the first line of the log file */
  83. if (line == 1)
  84. continue;
  85. /* break out of read loop */
  86. /* if we've passed the number of entries we want to read */
  87. if (line > 2)
  88. break;
  89. /* grab the timestamp */
  90. temp_buffer = strtok (input_buffer, " ");
  91. timestamp = strtoul (temp_buffer, NULL, 10);
  92. /* grab the average incoming transfer rate */
  93. temp_buffer = strtok (NULL, " ");
  94. average_incoming_rate = strtoul (temp_buffer, NULL, 10);
  95. /* grab the average outgoing transfer rate */
  96. temp_buffer = strtok (NULL, " ");
  97. average_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  98. /* grab the maximum incoming transfer rate */
  99. temp_buffer = strtok (NULL, " ");
  100. maximum_incoming_rate = strtoul (temp_buffer, NULL, 10);
  101. /* grab the maximum outgoing transfer rate */
  102. temp_buffer = strtok (NULL, " ");
  103. maximum_outgoing_rate = strtoul (temp_buffer, NULL, 10);
  104. }
  105. /* close the log file */
  106. fclose (fp);
  107. /* if we couldn't read enough data, return an unknown error */
  108. if (line <= 2)
  109. usage4 (_("Unable to process MRTG log file"));
  110. /* make sure the MRTG data isn't too old */
  111. time (&current_time);
  112. if ((expire_minutes > 0) &&
  113. (current_time - timestamp) > (expire_minutes * 60))
  114. die (STATE_WARNING, _("MRTG data has expired (%d minutes old)\n"),
  115. (int) ((current_time - timestamp) / 60));
  116. /* else check the incoming/outgoing rates */
  117. if (use_average == TRUE) {
  118. incoming_rate = average_incoming_rate;
  119. outgoing_rate = average_outgoing_rate;
  120. }
  121. else {
  122. incoming_rate = maximum_incoming_rate;
  123. outgoing_rate = maximum_outgoing_rate;
  124. }
  125. /* report incoming traffic in Bytes/sec */
  126. if (incoming_rate < 1024) {
  127. strcpy (incoming_speed_rating, "B/s");
  128. adjusted_incoming_rate = (double) incoming_rate;
  129. }
  130. /* report incoming traffic in KBytes/sec */
  131. else if (incoming_rate < (1024 * 1024)) {
  132. strcpy (incoming_speed_rating, "KB/s");
  133. adjusted_incoming_rate = (double) (incoming_rate / 1024.0);
  134. }
  135. /* report incoming traffic in MBytes/sec */
  136. else {
  137. strcpy (incoming_speed_rating, "MB/s");
  138. adjusted_incoming_rate = (double) (incoming_rate / 1024.0 / 1024.0);
  139. }
  140. /* report outgoing traffic in Bytes/sec */
  141. if (outgoing_rate < 1024) {
  142. strcpy (outgoing_speed_rating, "B/s");
  143. adjusted_outgoing_rate = (double) outgoing_rate;
  144. }
  145. /* report outgoing traffic in KBytes/sec */
  146. else if (outgoing_rate < (1024 * 1024)) {
  147. strcpy (outgoing_speed_rating, "KB/s");
  148. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0);
  149. }
  150. /* report outgoing traffic in MBytes/sec */
  151. else {
  152. strcpy (outgoing_speed_rating, "MB/s");
  153. adjusted_outgoing_rate = (double) (outgoing_rate / 1024.0 / 1024.0);
  154. }
  155. if (incoming_rate > incoming_critical_threshold
  156. || outgoing_rate > outgoing_critical_threshold) {
  157. result = STATE_CRITICAL;
  158. }
  159. else if (incoming_rate > incoming_warning_threshold
  160. || outgoing_rate > outgoing_warning_threshold) {
  161. result = STATE_WARNING;
  162. }
  163. asprintf (&error_message, _("%s. In = %0.1f %s, %s. Out = %0.1f %s|%s %s\n"),
  164. (use_average == TRUE) ? _("Avg") : _("Max"), adjusted_incoming_rate,
  165. incoming_speed_rating, (use_average == TRUE) ? _("Avg") : _("Max"),
  166. adjusted_outgoing_rate, outgoing_speed_rating,
  167. fperfdata("in", adjusted_incoming_rate, incoming_speed_rating,
  168. (int)incoming_warning_threshold, incoming_warning_threshold,
  169. (int)incoming_critical_threshold, incoming_critical_threshold,
  170. TRUE, 0, FALSE, 0),
  171. fperfdata("in", adjusted_outgoing_rate, outgoing_speed_rating,
  172. (int)outgoing_warning_threshold, outgoing_warning_threshold,
  173. (int)outgoing_critical_threshold, outgoing_critical_threshold,
  174. TRUE, 0, FALSE, 0));
  175. printf (_("Traffic %s - %s\n"), state_text(result), error_message);
  176. return result;
  177. }
  178. /* process command-line arguments */
  179. int
  180. process_arguments (int argc, char **argv)
  181. {
  182. int c;
  183. int option = 0;
  184. static struct option longopts[] = {
  185. {"logfile", required_argument, 0, 'F'},
  186. {"expires", required_argument, 0, 'e'},
  187. {"aggregation", required_argument, 0, 'a'},
  188. {"variable", required_argument, 0, 'v'},
  189. {"critical", required_argument, 0, 'c'},
  190. {"warning", required_argument, 0, 'w'},
  191. {"verbose", no_argument, 0, 'v'},
  192. {"version", no_argument, 0, 'V'},
  193. {"help", no_argument, 0, 'h'},
  194. {0, 0, 0, 0}
  195. };
  196. if (argc < 2)
  197. return ERROR;
  198. for (c = 1; c < argc; c++) {
  199. if (strcmp ("-to", argv[c]) == 0)
  200. strcpy (argv[c], "-t");
  201. else if (strcmp ("-wt", argv[c]) == 0)
  202. strcpy (argv[c], "-w");
  203. else if (strcmp ("-ct", argv[c]) == 0)
  204. strcpy (argv[c], "-c");
  205. }
  206. while (1) {
  207. c = getopt_long (argc, argv, "hVF:e:a:c:w:", longopts, &option);
  208. if (c == -1 || c == EOF)
  209. break;
  210. switch (c) {
  211. case 'F': /* input file */
  212. log_file = optarg;
  213. break;
  214. case 'e': /* expiration time */
  215. expire_minutes = atoi (optarg);
  216. break;
  217. case 'a': /* aggregation (AVE or MAX) */
  218. if (!strcmp (optarg, "MAX"))
  219. use_average = FALSE;
  220. else
  221. use_average = TRUE;
  222. break;
  223. case 'c': /* warning threshold */
  224. sscanf (optarg, "%lu,%lu", &incoming_critical_threshold,
  225. &outgoing_critical_threshold);
  226. break;
  227. case 'w': /* critical threshold */
  228. sscanf (optarg, "%lu,%lu", &incoming_warning_threshold,
  229. &outgoing_warning_threshold);
  230. break;
  231. case 'V': /* version */
  232. print_revision (progname, revision);
  233. exit (STATE_OK);
  234. case 'h': /* help */
  235. print_help ();
  236. exit (STATE_OK);
  237. case '?': /* help */
  238. usage5 ();
  239. }
  240. }
  241. c = optind;
  242. if (argc > c && log_file == NULL) {
  243. log_file = argv[c++];
  244. }
  245. if (argc > c && expire_minutes == -1) {
  246. expire_minutes = atoi (argv[c++]);
  247. }
  248. if (argc > c && strcmp (argv[c], "MAX") == 0) {
  249. use_average = FALSE;
  250. c++;
  251. }
  252. else if (argc > c && strcmp (argv[c], "AVG") == 0) {
  253. use_average = TRUE;
  254. c++;
  255. }
  256. if (argc > c && incoming_warning_threshold == 0) {
  257. incoming_warning_threshold = strtoul (argv[c++], NULL, 10);
  258. }
  259. if (argc > c && incoming_critical_threshold == 0) {
  260. incoming_critical_threshold = strtoul (argv[c++], NULL, 10);
  261. }
  262. if (argc > c && outgoing_warning_threshold == 0) {
  263. outgoing_warning_threshold = strtoul (argv[c++], NULL, 10);
  264. }
  265. if (argc > c && outgoing_critical_threshold == 0) {
  266. outgoing_critical_threshold = strtoul (argv[c++], NULL, 10);
  267. }
  268. return validate_arguments ();
  269. }
  270. int
  271. validate_arguments (void)
  272. {
  273. return OK;
  274. }
  275. void
  276. print_help (void)
  277. {
  278. print_revision (progname, revision);
  279. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  280. printf (COPYRIGHT, copyright, email);
  281. printf ("%s\n", _("This plugin will check the incoming/outgoing transfer rates of a router,"));
  282. printf ("%s\n", _("switch, etc recorded in an MRTG log. If the newest log entry is older"));
  283. printf ("%s\n", _("than <expire_minutes>, a WARNING status is returned. If either the"));
  284. printf ("%s\n", _("incoming or outgoing rates exceed the <icl> or <ocl> thresholds (in"));
  285. printf ("%s\n", _("Bytes/sec), a CRITICAL status results. If either of the rates exceed"));
  286. printf ("%s\n", _("the <iwl> or <owl> thresholds (in Bytes/sec), a WARNING status results."));
  287. printf ("\n\n");
  288. print_usage ();
  289. printf (_(UT_HELP_VRSN));
  290. printf (" %s\n", "-F, --filename=STRING");
  291. printf (" %s\n", _("File to read log from"));
  292. printf (" %s\n", "-e, --expires=INTEGER");
  293. printf (" %s\n", _("Minutes after which log expires"));
  294. printf (" %s\n", "-a, --aggregation=(AVG|MAX)");
  295. printf (" %s\n", _("Test average or maximum"));
  296. printf (" %s\n", "-w, --warning");
  297. printf (" %s\n", _("Warning threshold pair <incoming>,<outgoing>"));
  298. printf (" %s\n", "-c, --critical");
  299. printf (" %s\n", _("Critical threshold pair <incoming>,<outgoing>"));
  300. printf ("%s\n", _("Notes:"));
  301. printf (" %s\n", _("- MRTG stands for Multi Router Traffic Grapher. It can be downloaded from"));
  302. printf (" %s\n", " http://ee-staff.ethz.ch/~oetiker/webtools/mrtg/mrtg.html");
  303. printf (" %s\n", _("- While MRTG can monitor things other than traffic rates, this"));
  304. printf (" %s\n", _(" plugin probably won't work with much else without modification."));
  305. printf (" %s\n", _("- The calculated i/o rates are a little off from what MRTG actually"));
  306. printf (" %s\n", _(" reports. I'm not sure why this is right now, but will look into it"));
  307. printf (" %s\n", _(" for future enhancements of this plugin."));
  308. printf (_(UT_SUPPORT));
  309. }
  310. void
  311. print_usage (void)
  312. {
  313. printf (_("Usage"));
  314. printf (" %s -F <log_file> -a <AVG | MAX> -v <variable> -w <warning_pair>",progname);
  315. printf ("-c <critical_pair> [-e expire_minutes] [-t timeout] [-v]\n");
  316. }