summaryrefslogtreecommitdiffstats
path: root/lib/utils_base.c
diff options
context:
space:
mode:
authorLorenz Kästle <12514511+RincewindsHat@users.noreply.github.com>2023-10-15 19:01:25 +0200
committerGitHub <noreply@github.com>2023-10-15 19:01:25 +0200
commitfff1014d1323980d5e969f31a5a17132482422ac (patch)
tree0d0980d41f95385cc0137ec86e7a1cea0065e04c /lib/utils_base.c
parentb1801c10846283176622db21974fe5003602630d (diff)
parentddbabaa3b659bed9dcf5c5a2bfc430fb816277c7 (diff)
downloadmonitoring-plugins-fff1014d1323980d5e969f31a5a17132482422ac.tar.gz
Merge pull request #1937 from RincewindsHat/replace_booleans_in_lib
Replace old school booleans in lib
Diffstat (limited to 'lib/utils_base.c')
-rw-r--r--lib/utils_base.c75
1 files changed, 37 insertions, 38 deletions
diff --git a/lib/utils_base.c b/lib/utils_base.c
index 0f521263..3c7221c8 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -6,21 +6,21 @@
6* Copyright (c) 2006 Monitoring Plugins Development Team 6* Copyright (c) 2006 Monitoring Plugins Development Team
7* 7*
8* Library of useful functions for plugins 8* Library of useful functions for plugins
9* 9*
10* 10*
11* This program is free software: you can redistribute it and/or modify 11* This program is free software: you can redistribute it and/or modify
12* it under the terms of the GNU General Public License as published by 12* it under the terms of the GNU General Public License as published by
13* the Free Software Foundation, either version 3 of the License, or 13* the Free Software Foundation, either version 3 of the License, or
14* (at your option) any later version. 14* (at your option) any later version.
15* 15*
16* This program is distributed in the hope that it will be useful, 16* This program is distributed in the hope that it will be useful,
17* but WITHOUT ANY WARRANTY; without even the implied warranty of 17* but WITHOUT ANY WARRANTY; without even the implied warranty of
18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19* GNU General Public License for more details. 19* GNU General Public License for more details.
20* 20*
21* You should have received a copy of the GNU General Public License 21* You should have received a copy of the GNU General Public License
22* along with this program. If not, see <http://www.gnu.org/licenses/>. 22* along with this program. If not, see <http://www.gnu.org/licenses/>.
23* 23*
24* 24*
25*****************************************************************************/ 25*****************************************************************************/
26 26
@@ -40,7 +40,7 @@ monitoring_plugin *this_monitoring_plugin=NULL;
40unsigned int timeout_state = STATE_CRITICAL; 40unsigned int timeout_state = STATE_CRITICAL;
41unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT; 41unsigned int timeout_interval = DEFAULT_SOCKET_TIMEOUT;
42 42
43int _np_state_read_file(FILE *); 43bool _np_state_read_file(FILE *);
44 44
45void np_init( char *plugin_name, int argc, char **argv ) { 45void np_init( char *plugin_name, int argc, char **argv ) {
46 if (this_monitoring_plugin==NULL) { 46 if (this_monitoring_plugin==NULL) {
@@ -105,12 +105,12 @@ die (int result, const char *fmt, ...)
105 105
106void set_range_start (range *this, double value) { 106void set_range_start (range *this, double value) {
107 this->start = value; 107 this->start = value;
108 this->start_infinity = FALSE; 108 this->start_infinity = false;
109} 109}
110 110
111void set_range_end (range *this, double value) { 111void set_range_end (range *this, double value) {
112 this->end = value; 112 this->end = value;
113 this->end_infinity = FALSE; 113 this->end_infinity = false;
114} 114}
115 115
116range 116range
@@ -124,9 +124,9 @@ range
124 124
125 /* Set defaults */ 125 /* Set defaults */
126 temp_range->start = 0; 126 temp_range->start = 0;
127 temp_range->start_infinity = FALSE; 127 temp_range->start_infinity = false;
128 temp_range->end = 0; 128 temp_range->end = 0;
129 temp_range->end_infinity = TRUE; 129 temp_range->end_infinity = true;
130 temp_range->alert_on = OUTSIDE; 130 temp_range->alert_on = OUTSIDE;
131 temp_range->text = strdup(str); 131 temp_range->text = strdup(str);
132 132
@@ -138,7 +138,7 @@ range
138 end_str = index(str, ':'); 138 end_str = index(str, ':');
139 if (end_str != NULL) { 139 if (end_str != NULL) {
140 if (str[0] == '~') { 140 if (str[0] == '~') {
141 temp_range->start_infinity = TRUE; 141 temp_range->start_infinity = true;
142 } else { 142 } else {
143 start = strtod(str, NULL); /* Will stop at the ':' */ 143 start = strtod(str, NULL); /* Will stop at the ':' */
144 set_range_start(temp_range, start); 144 set_range_start(temp_range, start);
@@ -152,8 +152,8 @@ range
152 set_range_end(temp_range, end); 152 set_range_end(temp_range, end);
153 } 153 }
154 154
155 if (temp_range->start_infinity == TRUE || 155 if (temp_range->start_infinity == true ||
156 temp_range->end_infinity == TRUE || 156 temp_range->end_infinity == true ||
157 temp_range->start <= temp_range->end) { 157 temp_range->start <= temp_range->end) {
158 return temp_range; 158 return temp_range;
159 } 159 }
@@ -223,31 +223,30 @@ void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
223 printf("\n"); 223 printf("\n");
224} 224}
225 225
226/* Returns TRUE if alert should be raised based on the range */ 226/* Returns true if alert should be raised based on the range */
227int 227bool check_range(double value, range *my_range)
228check_range(double value, range *my_range)
229{ 228{
230 int no = FALSE; 229 bool no = false;
231 int yes = TRUE; 230 bool yes = true;
232 231
233 if (my_range->alert_on == INSIDE) { 232 if (my_range->alert_on == INSIDE) {
234 no = TRUE; 233 no = true;
235 yes = FALSE; 234 yes = false;
236 } 235 }
237 236
238 if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) { 237 if (my_range->end_infinity == false && my_range->start_infinity == false) {
239 if ((my_range->start <= value) && (value <= my_range->end)) { 238 if ((my_range->start <= value) && (value <= my_range->end)) {
240 return no; 239 return no;
241 } else { 240 } else {
242 return yes; 241 return yes;
243 } 242 }
244 } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) { 243 } else if (my_range->start_infinity == false && my_range->end_infinity == true) {
245 if (my_range->start <= value) { 244 if (my_range->start <= value) {
246 return no; 245 return no;
247 } else { 246 } else {
248 return yes; 247 return yes;
249 } 248 }
250 } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) { 249 } else if (my_range->start_infinity == true && my_range->end_infinity == false) {
251 if (value <= my_range->end) { 250 if (value <= my_range->end) {
252 return no; 251 return no;
253 } else { 252 } else {
@@ -263,12 +262,12 @@ int
263get_status(double value, thresholds *my_thresholds) 262get_status(double value, thresholds *my_thresholds)
264{ 263{
265 if (my_thresholds->critical != NULL) { 264 if (my_thresholds->critical != NULL) {
266 if (check_range(value, my_thresholds->critical) == TRUE) { 265 if (check_range(value, my_thresholds->critical) == true) {
267 return STATE_CRITICAL; 266 return STATE_CRITICAL;
268 } 267 }
269 } 268 }
270 if (my_thresholds->warning != NULL) { 269 if (my_thresholds->warning != NULL) {
271 if (check_range(value, my_thresholds->warning) == TRUE) { 270 if (check_range(value, my_thresholds->warning) == true) {
272 return STATE_WARNING; 271 return STATE_WARNING;
273 } 272 }
274 } 273 }
@@ -465,7 +464,7 @@ char* _np_state_calculate_location_prefix(){
465 464
466 /* Do not allow passing MP_STATE_PATH in setuid plugins 465 /* Do not allow passing MP_STATE_PATH in setuid plugins
467 * for security reasons */ 466 * for security reasons */
468 if (mp_suid() == FALSE) { 467 if (!mp_suid()) {
469 env_dir = getenv("MP_STATE_PATH"); 468 env_dir = getenv("MP_STATE_PATH");
470 if(env_dir && env_dir[0] != '\0') 469 if(env_dir && env_dir[0] != '\0')
471 return env_dir; 470 return env_dir;
@@ -541,7 +540,7 @@ void np_enable_state(char *keyname, int expected_data_version) {
541state_data *np_state_read() { 540state_data *np_state_read() {
542 state_data *this_state_data=NULL; 541 state_data *this_state_data=NULL;
543 FILE *statefile; 542 FILE *statefile;
544 int rc = FALSE; 543 bool rc = false;
545 544
546 if(this_monitoring_plugin==NULL) 545 if(this_monitoring_plugin==NULL)
547 die(STATE_UNKNOWN, _("This requires np_init to be called")); 546 die(STATE_UNKNOWN, _("This requires np_init to be called"));
@@ -563,7 +562,7 @@ state_data *np_state_read() {
563 fclose(statefile); 562 fclose(statefile);
564 } 563 }
565 564
566 if(rc==FALSE) { 565 if(!rc) {
567 _cleanup_state_data(); 566 _cleanup_state_data();
568 } 567 }
569 568
@@ -573,8 +572,8 @@ state_data *np_state_read() {
573/* 572/*
574 * Read the state file 573 * Read the state file
575 */ 574 */
576int _np_state_read_file(FILE *f) { 575bool _np_state_read_file(FILE *f) {
577 int status=FALSE; 576 bool status = false;
578 size_t pos; 577 size_t pos;
579 char *line; 578 char *line;
580 int i; 579 int i;
@@ -628,7 +627,7 @@ int _np_state_read_file(FILE *f) {
628 if(this_monitoring_plugin->state->state_data->data==NULL) 627 if(this_monitoring_plugin->state->state_data->data==NULL)
629 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno)); 628 die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
630 expected=STATE_DATA_END; 629 expected=STATE_DATA_END;
631 status=TRUE; 630 status=true;
632 break; 631 break;
633 case STATE_DATA_END: 632 case STATE_DATA_END:
634 ; 633 ;
@@ -640,10 +639,10 @@ int _np_state_read_file(FILE *f) {
640} 639}
641 640
642/* 641/*
643 * If time=NULL, use current time. Create state file, with state format 642 * If time=NULL, use current time. Create state file, with state format
644 * version, default text. Writes version, time, and data. Avoid locking 643 * version, default text. Writes version, time, and data. Avoid locking
645 * problems - use mv to write and then swap. Possible loss of state data if 644 * problems - use mv to write and then swap. Possible loss of state data if
646 * two things writing to same key at same time. 645 * two things writing to same key at same time.
647 * Will die with UNKNOWN if errors 646 * Will die with UNKNOWN if errors
648 */ 647 */
649void np_state_write_string(time_t data_time, char *data_string) { 648void np_state_write_string(time_t data_time, char *data_string) {
@@ -658,7 +657,7 @@ void np_state_write_string(time_t data_time, char *data_string) {
658 time(&current_time); 657 time(&current_time);
659 else 658 else
660 current_time=data_time; 659 current_time=data_time;
661 660
662 /* If file doesn't currently exist, create directories */ 661 /* If file doesn't currently exist, create directories */
663 if(access(this_monitoring_plugin->state->_filename,F_OK)!=0) { 662 if(access(this_monitoring_plugin->state->_filename,F_OK)!=0) {
664 result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename); 663 result = asprintf(&directories, "%s", this_monitoring_plugin->state->_filename);
@@ -697,15 +696,15 @@ void np_state_write_string(time_t data_time, char *data_string) {
697 np_free(temp_file); 696 np_free(temp_file);
698 die(STATE_UNKNOWN, _("Unable to open temporary state file")); 697 die(STATE_UNKNOWN, _("Unable to open temporary state file"));
699 } 698 }
700 699
701 fprintf(fp,"# NP State file\n"); 700 fprintf(fp,"# NP State file\n");
702 fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION); 701 fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION);
703 fprintf(fp,"%d\n",this_monitoring_plugin->state->data_version); 702 fprintf(fp,"%d\n",this_monitoring_plugin->state->data_version);
704 fprintf(fp,"%lu\n",current_time); 703 fprintf(fp,"%lu\n",current_time);
705 fprintf(fp,"%s\n",data_string); 704 fprintf(fp,"%s\n",data_string);
706 705
707 fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP); 706 fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP);
708 707
709 fflush(fp); 708 fflush(fp);
710 709
711 result=fclose(fp); 710 result=fclose(fp);