check_uptime.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. int validate_arguments (void);
  38. int getuptime (void);
  39. void print_help (void);
  40. void print_usage (void);
  41. int verbose = 0;
  42. char *warning, *critical, *timeunit;
  43. thresholds *my_thresholds = NULL;
  44. int main (int argc, char **argv) {
  45. // base values
  46. int status, intvalue;
  47. int upminutes, uphours, updays;
  48. double value, uptime;
  49. char* perf;
  50. char* output_message;
  51. /* Parse extra opts if any */
  52. argv = np_extra_opts (&argc, argv, progname);
  53. if (process_arguments (argc, argv) == ERROR)
  54. usage4 (_("Could not parse arguments"));
  55. /* Set signal handling and alarm timeout */
  56. if (signal (SIGALRM, timeout_alarm_handler) == SIG_ERR) {
  57. die (STATE_UNKNOWN, _("Cannot catch SIGALRM"));
  58. }
  59. alarm (timeout_interval);
  60. value = getuptime();
  61. if (verbose >= 3) {
  62. printf("Uptime in seconds returned from timespec struct: %f\n", value);
  63. }
  64. intvalue = (int)value;
  65. updays = intvalue / 86400;
  66. uphours = (intvalue % 86400) / 3600;
  67. upminutes = ((intvalue % 86400) % 3600) / 60;
  68. if (!strncmp(timeunit, "minutes", strlen("minutes"))) {
  69. uptime = intvalue / 60;
  70. } else if (!strncmp(timeunit, "hours", strlen("hours"))) {
  71. uptime = intvalue / 3600;
  72. } else if (!strncmp(timeunit, "days", strlen("days"))) {
  73. uptime = intvalue / 86400;
  74. } else {
  75. uptime = intvalue;
  76. }
  77. xasprintf(&output_message,_("%u day(s) %u hour(s) %u minute(s)"), updays, uphours, upminutes);
  78. xasprintf(&perf,_("%s"),
  79. fperfdata("uptime", uptime, "",
  80. my_thresholds->warning?TRUE:FALSE, my_thresholds->warning?my_thresholds->warning->end:0,
  81. my_thresholds->critical?TRUE:FALSE, my_thresholds->critical?my_thresholds->critical->end:0,
  82. FALSE, 0,
  83. FALSE, 0)
  84. );
  85. status = get_status(uptime, my_thresholds);
  86. if (status == STATE_OK) {
  87. printf("Uptime %s: %s | %s\n", _("OK"), output_message, perf);
  88. } else if (status == STATE_WARNING) {
  89. printf("Uptime %s: %s | %s\n", _("WARNING"), output_message, perf);
  90. } else if (status == STATE_CRITICAL) {
  91. printf("Uptime %s: %s | %s\n", _("CRITICAL"), output_message, perf);
  92. }
  93. return status;
  94. } // end main
  95. int getuptime () {
  96. struct timespec t;
  97. clock_gettime(CLOCK_MONOTONIC, &t);
  98. if (t.tv_sec > 0) {
  99. return t.tv_sec;
  100. } else {
  101. printf("Uptime UNKNOWN: Timespec struct failed to retrieve uptime\n");
  102. exit (STATE_UNKNOWN);
  103. }
  104. } // end getuptime
  105. static int process_arguments (int argc, char **argv) {
  106. int c;
  107. int escape = 0;
  108. char *temp;
  109. int option = 0;
  110. static struct option longopts[] = {
  111. {"critical", required_argument, 0, 'c'},
  112. {"warning", required_argument, 0, 'w'},
  113. {"timeout", required_argument, 0, 't'},
  114. {"timeunit", required_argument, 0, 'u'},
  115. {"verbose", no_argument, 0, 'v'},
  116. {"version", no_argument, 0, 'V'},
  117. {"help", no_argument, 0, 'h'},
  118. {0, 0, 0, 0}
  119. };
  120. while ( 1 ) {
  121. c = getopt_long ( argc, argv, "+hvVu:c:w:t:", longopts, &option );
  122. if ( c == -1 || c == EOF || c == 1 ) break;
  123. switch ( c ) {
  124. case '?':
  125. usage5 ();
  126. case 'h':
  127. print_help ();
  128. exit ( STATE_OK );
  129. case 'v':
  130. verbose++;
  131. if (verbose >= 3) {
  132. printf("Verbose mode enabled\n");
  133. }
  134. break;
  135. case 'V':
  136. print_revision (progname, NP_VERSION);
  137. exit (STATE_OK);
  138. case 'u':
  139. timeunit = optarg;
  140. break;
  141. case 'c':
  142. critical = optarg;
  143. break;
  144. case 'w':
  145. warning = optarg;
  146. break;
  147. case 't': /* timeout period */
  148. timeout_interval = parse_timeout_string (optarg);
  149. break;
  150. } // end case
  151. } // end while
  152. set_thresholds(&my_thresholds, warning, critical);
  153. return validate_arguments ();
  154. } // end process_arguments
  155. int validate_arguments (void) {
  156. if (timeunit == NULL) {
  157. timeunit = "minutes";
  158. if (verbose >= 3) {
  159. printf("No unit of time measurement specified. Using default: \"minutes\"\n");
  160. }
  161. } else if (strncmp(timeunit, "seconds", strlen("seconds") + 1 ) &&
  162. strncmp(timeunit, "minutes", strlen("minutes") + 1) &&
  163. strncmp(timeunit, "hours", strlen("hours") + 1) &&
  164. strncmp(timeunit, "days", strlen("days") + 1)) {
  165. if (verbose >= 3) {
  166. printf("Invalid unit of time measurement specified: \"%s\"\n", timeunit);
  167. }
  168. usage4(_("Wrong -u argument, expected: seconds, minutes, hours, or days"));
  169. } else if (verbose >= 3) {
  170. printf("Specified unit of time measurement accepted: \"%s\"\n", timeunit);
  171. }
  172. return OK;
  173. } //end validate
  174. void print_usage (void) {
  175. printf( "%s\n", _("Usage:") );
  176. printf( "%s", _("check_uptime ") );
  177. printf( "%s\n", _("[-u uom] [-w threshold] [-c threshold] [-t] [-h] [-vvv] [-V]") );
  178. } // end usage
  179. void print_help (void) {
  180. print_revision ( progname, NP_VERSION );
  181. printf ( COPYRIGHT, copyright, developer, email );
  182. printf ( "%s\n", _("This plugin checks the system uptime and alerts if less than the threshold.") );
  183. printf ( "%s\n", _("Threshold unit of measurement specified with \"-u\".") );
  184. printf ( "%s\n", _("\"-u\" switch supports: seconds|minutes|hours|days.") );
  185. print_usage ();
  186. printf ( UT_HELP_VRSN );
  187. printf ( UT_EXTRA_OPTS );
  188. printf ( "%s\n", _("-t, Plugin timeout, default 10 seconds") );
  189. printf ( "%s\n", _("-c, Critcal threshold") );
  190. printf ( "%s\n", _("-w, Warning threshold") );
  191. printf ( "%s\n", _("-u, Time unit of measurement (seconds|minutes|hours|days) (default: minutes)") );
  192. printf ( "%s\n", _("-vvv, Enable verbose output") );
  193. //printf ( "%s\n", _("-h, Print help and usage") );
  194. printf ( UT_SUPPORT );
  195. } // end print_help