check_mrtgtraf.c 10 KB

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