check_uptime.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*********************************************************************************
  2. *
  3. * Nagios check_uptime plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2013-2014 Nagios Plugin Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_uptime plugin
  11. *
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation, either version 3 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. *********************************************************************************/
  27. #include "common.h"
  28. #include "utils.h"
  29. #include "utils_base.h"
  30. #include <time.h>
  31. char *progname = "check_uptime";
  32. char *version = "1.0";
  33. char *email = "devel@nagios-plugins.org";
  34. char *copyright = "2014";
  35. char *developer = "Andy Brist";
  36. static int process_arguments (int, char **);
  37. void print_help (void);
  38. void print_usage (void);
  39. int verbose = 0;
  40. char *warning, *critical, *timeunit;
  41. thresholds *my_thresholds = NULL;
  42. int main (int argc, char **argv) {
  43. // base values
  44. int status, intvalue;
  45. int upminutes, uphours, updays;
  46. double value, uptime;
  47. char* perf;
  48. char* output_message;
  49. /* Parse extra opts if any */
  50. argv = np_extra_opts (&argc, argv, progname);
  51. if (process_arguments (argc, argv) == ERROR)
  52. usage4 (_("Could not parse arguments"));
  53. value = getuptime();
  54. if (verbose >= 3) {
  55. printf("Uptime in seconds returned from timespec struct: %f\n", value);
  56. }
  57. intvalue = (int)value;
  58. updays = intvalue / 86400;
  59. uphours = (intvalue % 86400) / 3600;
  60. upminutes = ((intvalue % 86400) % 3600) / 60;
  61. if (!strncmp(timeunit, "minutes", strlen("minutes"))) {
  62. uptime = intvalue / 60;
  63. } else if (!strncmp(timeunit, "hours", strlen("hours"))) {
  64. uptime = intvalue / 3600;
  65. } else if (!strncmp(timeunit, "days", strlen("days"))) {
  66. uptime = intvalue / 86400;
  67. } else {
  68. uptime = intvalue;
  69. }
  70. xasprintf(&output_message,_("%u day(s) %u hour(s) %u minute(s)"), updays, uphours, upminutes);
  71. xasprintf(&perf,_("%s"),
  72. fperfdata("uptime", uptime, "",
  73. my_thresholds->warning?TRUE:FALSE, my_thresholds->warning?my_thresholds->warning->end:0,
  74. my_thresholds->critical?TRUE:FALSE, my_thresholds->critical?my_thresholds->critical->end:0,
  75. FALSE, 0,
  76. FALSE, 0)
  77. );
  78. status = get_status(uptime, my_thresholds);
  79. if (status == STATE_OK) {
  80. printf("Uptime %s: %s | %s\n", _("OK"), output_message, perf);
  81. } else if (status == STATE_WARNING) {
  82. printf("Uptime %s: %s | %s\n", _("WARNING"), output_message, perf);
  83. } else if (status == STATE_CRITICAL) {
  84. printf("Uptime %s: %s | %s\n", _("CRITICAL"), output_message, perf);
  85. }
  86. return status;
  87. } // end main
  88. int getuptime () {
  89. struct timespec t;
  90. clock_gettime(CLOCK_MONOTONIC, &t);
  91. if (t.tv_sec > 0) {
  92. return t.tv_sec;
  93. } else {
  94. printf("Uptime UNKNOWN: Timespec struct failed to retrieve uptime\n");
  95. exit (STATE_UNKNOWN);
  96. }
  97. } // end getuptime
  98. static int process_arguments (int argc, char **argv) {
  99. int c;
  100. int escape = 0;
  101. char *temp;
  102. int option = 0;
  103. static struct option longopts[] = {
  104. {"critical", required_argument, 0, 'c'},
  105. {"warning", required_argument, 0, 'w'},
  106. {"timeunit", required_argument, 0, 'u'},
  107. {"verbose", no_argument, 0, 'v'},
  108. {"version", no_argument, 0, 'V'},
  109. {"help", no_argument, 0, 'h'},
  110. {0, 0, 0, 0}
  111. };
  112. while ( 1 ) {
  113. c = getopt_long ( argc, argv, "+hvVu:c:w:", longopts, &option );
  114. if ( c == -1 || c == EOF || c == 1 ) break;
  115. switch ( c ) {
  116. case '?':
  117. usage5 ();
  118. case 'h':
  119. print_help ();
  120. exit ( STATE_OK );
  121. case 'v':
  122. verbose++;
  123. if (verbose >= 3) {
  124. printf("Verbose mode enabled\n");
  125. }
  126. break;
  127. case 'V':
  128. print_revision (progname, NP_VERSION);
  129. exit (STATE_OK);
  130. case 'u':
  131. timeunit = optarg;
  132. break;
  133. case 'c':
  134. critical = optarg;
  135. break;
  136. case 'w':
  137. warning = optarg;
  138. break;
  139. } // end case
  140. } // end while
  141. c = optind;
  142. set_thresholds(&my_thresholds, warning, critical);
  143. return validate_arguments ();
  144. } // end process_arguments
  145. int validate_arguments (void) {
  146. if (timeunit == NULL) {
  147. timeunit = "minutes";
  148. if (verbose >= 3) {
  149. printf("No unit of time measurement specified. Using default: \"minutes\"\n");
  150. }
  151. } else if (strncmp(timeunit, "seconds", strlen("seconds") + 1 ) &&
  152. strncmp(timeunit, "minutes", strlen("minutes") + 1) &&
  153. strncmp(timeunit, "hours", strlen("hours") + 1) &&
  154. strncmp(timeunit, "days", strlen("days") + 1)) {
  155. if (verbose >= 3) {
  156. printf("Invalid unit of time measurement specified: \"%s\"\n", timeunit);
  157. }
  158. usage4(_("Wrong -u argument, expected: seconds, minutes, hours, or days"));
  159. } else if (verbose >= 3) {
  160. printf("Specified unit of time measurement accepted: \"%s\"\n", timeunit);
  161. }
  162. return OK;
  163. } //end validate
  164. void print_usage (void) {
  165. printf( "%s\n", _("Usage:") );
  166. printf( "%s", _("check_uptime ") );
  167. printf( "%s\n", _("[-u uom] [-w threshold] [-c threshold] [-h] [-v] [-V]") );
  168. } // end usage
  169. void print_help (void) {
  170. print_revision ( progname, NP_VERSION );
  171. printf ( COPYRIGHT, copyright, developer, email );
  172. printf ( "%s\n", _("This plugin checks the system uptime and alerts if less than the threshold.") );
  173. printf ( "%s\n", _("Threshold unit of measurement specified with \"-u\".") );
  174. printf ( "%s\n", _("\"-u\" switch supports: seconds|minutes|hours|days.") );
  175. print_usage ();
  176. printf ( UT_HELP_VRSN );
  177. printf ( UT_EXTRA_OPTS );
  178. printf ( "%s\n", _("-t, Connection timeout, default 10 seconds") );
  179. printf ( "%s\n", _("-c, Critcal threshold") );
  180. printf ( "%s\n", _("-w, Warning threshold") );
  181. printf ( "%s\n", _("-u, Time unit of measurement (seconds|minutes|hours|days) (default: minutes)") );
  182. printf ( "%s\n", _("-v, Enable verbose output") );
  183. printf ( "%s\n", _("-h, Print help and usage") );
  184. printf ( UT_SUPPORT );
  185. } // end print_help