utils_base.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*****************************************************************************
  2. *
  3. * utils_base.c
  4. *
  5. * License: GPL
  6. * Copyright (c) 2006-2014 Nagios Plugins Development Team
  7. *
  8. * Library of useful functions for plugins
  9. *
  10. *
  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
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  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/>.
  23. *
  24. *
  25. *****************************************************************************/
  26. #include "common.h"
  27. #include <stdarg.h>
  28. #include "utils_base.h"
  29. #include <ctype.h>
  30. #include <fcntl.h>
  31. #include <sys/stat.h>
  32. #include <unistd.h>
  33. #include <sys/types.h>
  34. #define np_free(ptr) { if(ptr) { free(ptr); ptr = NULL; } }
  35. nagios_plugin *this_nagios_plugin=NULL;
  36. int _np_state_read_file(FILE *);
  37. void np_init( char *plugin_name, int argc, char **argv ) {
  38. if (this_nagios_plugin==NULL) {
  39. this_nagios_plugin = calloc(1, sizeof(nagios_plugin));
  40. if (this_nagios_plugin==NULL) {
  41. die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
  42. strerror(errno));
  43. }
  44. this_nagios_plugin->plugin_name = strdup(plugin_name);
  45. if (this_nagios_plugin->plugin_name==NULL)
  46. die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
  47. this_nagios_plugin->argc = argc;
  48. this_nagios_plugin->argv = argv;
  49. }
  50. }
  51. void np_set_args( int argc, char **argv ) {
  52. if (this_nagios_plugin==NULL)
  53. die(STATE_UNKNOWN, _("This requires np_init to be called"));
  54. this_nagios_plugin->argc = argc;
  55. this_nagios_plugin->argv = argv;
  56. }
  57. void np_cleanup() {
  58. if (this_nagios_plugin!=NULL) {
  59. if(this_nagios_plugin->state!=NULL) {
  60. if(this_nagios_plugin->state->state_data) {
  61. np_free(this_nagios_plugin->state->state_data->data);
  62. np_free(this_nagios_plugin->state->state_data);
  63. }
  64. np_free(this_nagios_plugin->state->name);
  65. np_free(this_nagios_plugin->state);
  66. }
  67. np_free(this_nagios_plugin->plugin_name);
  68. np_free(this_nagios_plugin);
  69. }
  70. this_nagios_plugin=NULL;
  71. }
  72. /* Hidden function to get a pointer to this_nagios_plugin for testing */
  73. void _get_nagios_plugin( nagios_plugin **pointer ){
  74. *pointer = this_nagios_plugin;
  75. }
  76. void
  77. die (int result, const char *fmt, ...)
  78. {
  79. va_list ap;
  80. va_start (ap, fmt);
  81. vprintf (fmt, ap);
  82. va_end (ap);
  83. if(this_nagios_plugin!=NULL) {
  84. np_cleanup();
  85. }
  86. exit (result);
  87. }
  88. void set_range_start (range *this, double value) {
  89. this->start = value;
  90. this->start_infinity = FALSE;
  91. }
  92. void set_range_end (range *this, double value) {
  93. this->end = value;
  94. this->end_infinity = FALSE;
  95. }
  96. range
  97. *parse_range_string (char *str) {
  98. range *temp_range;
  99. double start;
  100. double end;
  101. char *end_str;
  102. temp_range = (range *) calloc(1, sizeof(range));
  103. /* Set defaults */
  104. temp_range->start = 0;
  105. temp_range->start_infinity = FALSE;
  106. temp_range->end = 0;
  107. temp_range->end_infinity = TRUE;
  108. temp_range->alert_on = OUTSIDE;
  109. if (str[0] == '@') {
  110. temp_range->alert_on = INSIDE;
  111. str++;
  112. }
  113. end_str = index(str, ':');
  114. if (end_str != NULL) {
  115. if (str[0] == '~') {
  116. temp_range->start_infinity = TRUE;
  117. } else {
  118. start = strtod(str, NULL); /* Will stop at the ':' */
  119. set_range_start(temp_range, start);
  120. }
  121. end_str++; /* Move past the ':' */
  122. } else {
  123. end_str = str;
  124. }
  125. end = strtod(end_str, NULL);
  126. if (strcmp(end_str, "") != 0) {
  127. set_range_end(temp_range, end);
  128. }
  129. if (temp_range->start_infinity == TRUE ||
  130. temp_range->end_infinity == TRUE ||
  131. temp_range->start <= temp_range->end) {
  132. return temp_range;
  133. }
  134. free(temp_range);
  135. return NULL;
  136. }
  137. /* returns 0 if okay, otherwise 1 */
  138. int
  139. _set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
  140. {
  141. thresholds *temp_thresholds = NULL;
  142. if ((temp_thresholds = calloc(1, sizeof(thresholds))) == NULL)
  143. die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
  144. strerror(errno));
  145. temp_thresholds->warning = NULL;
  146. temp_thresholds->critical = NULL;
  147. if (warn_string != NULL) {
  148. if ((temp_thresholds->warning = parse_range_string(warn_string)) == NULL) {
  149. return NP_RANGE_UNPARSEABLE;
  150. }
  151. }
  152. if (critical_string != NULL) {
  153. if ((temp_thresholds->critical = parse_range_string(critical_string)) == NULL) {
  154. return NP_RANGE_UNPARSEABLE;
  155. }
  156. }
  157. *my_thresholds = temp_thresholds;
  158. return 0;
  159. }
  160. void
  161. set_thresholds(thresholds **my_thresholds, char *warn_string, char *critical_string)
  162. {
  163. switch (_set_thresholds(my_thresholds, warn_string, critical_string)) {
  164. case 0:
  165. return;
  166. case NP_RANGE_UNPARSEABLE:
  167. die(STATE_UNKNOWN, _("Range format incorrect"));
  168. case NP_WARN_WITHIN_CRIT:
  169. die(STATE_UNKNOWN, _("Warning level is a subset of critical and will not be alerted"));
  170. break;
  171. }
  172. }
  173. void print_thresholds(const char *threshold_name, thresholds *my_threshold) {
  174. printf("%s - ", threshold_name);
  175. if (! my_threshold) {
  176. printf("Threshold not set");
  177. } else {
  178. if (my_threshold->warning) {
  179. printf("Warning: start=%g end=%g; ", my_threshold->warning->start, my_threshold->warning->end);
  180. } else {
  181. printf("Warning not set; ");
  182. }
  183. if (my_threshold->critical) {
  184. printf("Critical: start=%g end=%g", my_threshold->critical->start, my_threshold->critical->end);
  185. } else {
  186. printf("Critical not set");
  187. }
  188. }
  189. printf("\n");
  190. }
  191. /* Returns TRUE if alert should be raised based on the range */
  192. int
  193. check_range(double value, range *my_range)
  194. {
  195. int no = FALSE;
  196. int yes = TRUE;
  197. if (my_range->alert_on == INSIDE) {
  198. no = TRUE;
  199. yes = FALSE;
  200. }
  201. if (my_range->end_infinity == FALSE && my_range->start_infinity == FALSE) {
  202. if ((my_range->start <= value) && (value <= my_range->end)) {
  203. return no;
  204. } else {
  205. return yes;
  206. }
  207. } else if (my_range->start_infinity == FALSE && my_range->end_infinity == TRUE) {
  208. if (my_range->start <= value) {
  209. return no;
  210. } else {
  211. return yes;
  212. }
  213. } else if (my_range->start_infinity == TRUE && my_range->end_infinity == FALSE) {
  214. if (value <= my_range->end) {
  215. return no;
  216. } else {
  217. return yes;
  218. }
  219. } else {
  220. return no;
  221. }
  222. }
  223. /* Returns status */
  224. int
  225. get_status(double value, thresholds *my_thresholds)
  226. {
  227. if (my_thresholds->critical != NULL) {
  228. if (check_range(value, my_thresholds->critical) == TRUE) {
  229. return STATE_CRITICAL;
  230. }
  231. }
  232. if (my_thresholds->warning != NULL) {
  233. if (check_range(value, my_thresholds->warning) == TRUE) {
  234. return STATE_WARNING;
  235. }
  236. }
  237. return STATE_OK;
  238. }
  239. char *np_escaped_string (const char *string) {
  240. char *data;
  241. int i, j=0;
  242. data = strdup(string);
  243. for (i=0; data[i]; i++) {
  244. if (data[i] == '\\') {
  245. switch(data[++i]) {
  246. case 'n':
  247. data[j++] = '\n';
  248. break;
  249. case 'r':
  250. data[j++] = '\r';
  251. break;
  252. case 't':
  253. data[j++] = '\t';
  254. break;
  255. case '\\':
  256. data[j++] = '\\';
  257. break;
  258. default:
  259. data[j++] = data[i];
  260. }
  261. } else {
  262. data[j++] = data[i];
  263. }
  264. }
  265. data[j] = '\0';
  266. return data;
  267. }
  268. int np_check_if_root(void) { return (geteuid() == 0); }
  269. int np_warn_if_not_root(void) {
  270. int status = np_check_if_root();
  271. if(!status) {
  272. printf(_("Warning: "));
  273. printf(_("This plugin must be either run as root or setuid root.\n"));
  274. printf(_("To run as root, you can use a tool like sudo.\n"));
  275. printf(_("To set the setuid permissions, use the command:\n"));
  276. /* XXX could we use something like progname? */
  277. printf("\tchmod u+s yourpluginfile\n");
  278. }
  279. return status;
  280. }
  281. /*
  282. * Extract the value from key/value pairs, or return NULL. The value returned
  283. * can be free()ed.
  284. * This function can be used to parse NTP control packet data and performance
  285. * data strings.
  286. */
  287. char *np_extract_value(const char *varlist, const char *name, char sep) {
  288. char *tmp=NULL, *value=NULL;
  289. int i;
  290. while (1) {
  291. /* Strip any leading space */
  292. for (varlist; isspace(varlist[0]); varlist++);
  293. if (strncmp(name, varlist, strlen(name)) == 0) {
  294. varlist += strlen(name);
  295. /* strip trailing spaces */
  296. for (varlist; isspace(varlist[0]); varlist++);
  297. if (varlist[0] == '=') {
  298. /* We matched the key, go past the = sign */
  299. varlist++;
  300. /* strip leading spaces */
  301. for (varlist; isspace(varlist[0]); varlist++);
  302. if (tmp = index(varlist, sep)) {
  303. /* Value is delimited by a comma */
  304. if (tmp-varlist == 0) continue;
  305. value = (char *)calloc(1, tmp-varlist+1);
  306. strncpy(value, varlist, tmp-varlist);
  307. value[tmp-varlist] = '\0';
  308. } else {
  309. /* Value is delimited by a \0 */
  310. if (strlen(varlist) == 0) continue;
  311. value = (char *)calloc(1, strlen(varlist) + 1);
  312. strncpy(value, varlist, strlen(varlist));
  313. value[strlen(varlist)] = '\0';
  314. }
  315. break;
  316. }
  317. }
  318. if (tmp = index(varlist, sep)) {
  319. /* More keys, keep going... */
  320. varlist = tmp + 1;
  321. } else {
  322. /* We're done */
  323. break;
  324. }
  325. }
  326. /* Clean-up trailing spaces/newlines */
  327. if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0';
  328. return value;
  329. }
  330. /*
  331. * Read a string representing a state (ok, warning... or numeric: 0, 1) and
  332. * return the corresponding STATE_ value or ERROR)
  333. */
  334. int translate_state (char *state_text) {
  335. if (!strcasecmp(state_text,"OK") || !strcmp(state_text,"0"))
  336. return STATE_OK;
  337. if (!strcasecmp(state_text,"WARNING") || !strcmp(state_text,"1"))
  338. return STATE_WARNING;
  339. if (!strcasecmp(state_text,"CRITICAL") || !strcmp(state_text,"2"))
  340. return STATE_CRITICAL;
  341. if (!strcasecmp(state_text,"UNKNOWN") || !strcmp(state_text,"3"))
  342. return STATE_UNKNOWN;
  343. return ERROR;
  344. }
  345. /*
  346. * Returns a string to use as a keyname, based on an md5 hash of argv, thus
  347. * hopefully a unique key per service/plugin invocation. Use the extra-opts
  348. * parse of argv, so that uniqueness in parameters are reflected there.
  349. */
  350. char *_np_state_generate_key() {
  351. struct sha1_ctx ctx;
  352. int i;
  353. char **argv = this_nagios_plugin->argv;
  354. unsigned char result[20];
  355. char keyname[41];
  356. char *p=NULL;
  357. sha1_init_ctx(&ctx);
  358. for(i=0; i<this_nagios_plugin->argc; i++) {
  359. sha1_process_bytes(argv[i], strlen(argv[i]), &ctx);
  360. }
  361. sha1_finish_ctx(&ctx, &result);
  362. for (i=0; i<20; ++i) {
  363. sprintf(&keyname[2*i], "%02x", result[i]);
  364. }
  365. keyname[40]='\0';
  366. p = strdup(keyname);
  367. if(p==NULL) {
  368. die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
  369. }
  370. return p;
  371. }
  372. void _cleanup_state_data() {
  373. if (this_nagios_plugin->state->state_data!=NULL) {
  374. np_free(this_nagios_plugin->state->state_data->data);
  375. np_free(this_nagios_plugin->state->state_data);
  376. }
  377. }
  378. /*
  379. * Internal function. Returns either:
  380. * envvar NAGIOS_PLUGIN_STATE_DIRECTORY
  381. * statically compiled shared state directory
  382. */
  383. char* _np_state_calculate_location_prefix(){
  384. char *env_dir;
  385. /* Do not allow passing NP_STATE_DIRECTORY in setuid plugins
  386. * for security reasons */
  387. if (np_suid() == FALSE) {
  388. env_dir = getenv("NAGIOS_PLUGIN_STATE_DIRECTORY");
  389. if(env_dir && env_dir[0] != '\0')
  390. return env_dir;
  391. return NP_STATE_DIR_PREFIX;
  392. }
  393. }
  394. /*
  395. * Initiatializer for state routines.
  396. * Sets variables. Generates filename. Returns np_state_key. die with
  397. * UNKNOWN if exception
  398. */
  399. void np_enable_state(char *keyname, int expected_data_version) {
  400. state_key *this_state = NULL;
  401. char *temp_filename = NULL;
  402. char *temp_keyname = NULL;
  403. char *p=NULL;
  404. int ret;
  405. if(this_nagios_plugin==NULL)
  406. die(STATE_UNKNOWN, _("This requires np_init to be called"));
  407. this_state = (state_key *) calloc(1, sizeof(state_key));
  408. if(this_state==NULL)
  409. die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
  410. strerror(errno));
  411. if(keyname==NULL) {
  412. temp_keyname = _np_state_generate_key();
  413. } else {
  414. temp_keyname = strdup(keyname);
  415. if(temp_keyname==NULL)
  416. die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
  417. }
  418. /* Die if invalid characters used for keyname */
  419. p = temp_keyname;
  420. while(*p!='\0') {
  421. if(! (isalnum(*p) || *p == '_')) {
  422. die(STATE_UNKNOWN, _("Invalid character for keyname - only alphanumerics or '_'"));
  423. }
  424. p++;
  425. }
  426. this_state->name=temp_keyname;
  427. this_state->plugin_name=this_nagios_plugin->plugin_name;
  428. this_state->data_version=expected_data_version;
  429. this_state->state_data=NULL;
  430. /* Calculate filename */
  431. ret = asprintf(&temp_filename, "%s/%s/%s", _np_state_calculate_location_prefix(), this_nagios_plugin->plugin_name, this_state->name);
  432. if (ret < 0)
  433. die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
  434. this_state->_filename=temp_filename;
  435. this_nagios_plugin->state = this_state;
  436. }
  437. /*
  438. * Will return NULL if no data is available (first run). If key currently
  439. * exists, read data. If state file format version is not expected, return
  440. * as if no data. Get state data version number and compares to expected.
  441. * If numerically lower, then return as no previous state. die with UNKNOWN
  442. * if exceptional error.
  443. */
  444. state_data *np_state_read() {
  445. state_data *this_state_data=NULL;
  446. FILE *statefile;
  447. int rc = FALSE;
  448. if(this_nagios_plugin==NULL)
  449. die(STATE_UNKNOWN, _("This requires np_init to be called"));
  450. /* Open file. If this fails, no previous state found */
  451. statefile = fopen( this_nagios_plugin->state->_filename, "r" );
  452. if(statefile!=NULL) {
  453. this_state_data = (state_data *) calloc(1, sizeof(state_data));
  454. if(this_state_data==NULL)
  455. die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
  456. strerror(errno));
  457. this_state_data->data=NULL;
  458. this_nagios_plugin->state->state_data = this_state_data;
  459. rc = _np_state_read_file(statefile);
  460. fclose(statefile);
  461. }
  462. if(rc==FALSE) {
  463. _cleanup_state_data();
  464. }
  465. return this_nagios_plugin->state->state_data;
  466. }
  467. /*
  468. * Read the state file
  469. */
  470. int _np_state_read_file(FILE *f) {
  471. int status=FALSE;
  472. size_t pos;
  473. char *line;
  474. int i;
  475. int failure=0;
  476. time_t current_time, data_time;
  477. enum { STATE_FILE_VERSION, STATE_DATA_VERSION, STATE_DATA_TIME, STATE_DATA_TEXT, STATE_DATA_END } expected=STATE_FILE_VERSION;
  478. time(&current_time);
  479. /* Note: This introduces a limit of 1024 bytes in the string data */
  480. line = (char *) calloc(1, 1024);
  481. if(line==NULL)
  482. die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
  483. strerror(errno));
  484. while(!failure && (fgets(line,1024,f))!=NULL){
  485. pos=strlen(line);
  486. if(line[pos-1]=='\n') {
  487. line[pos-1]='\0';
  488. }
  489. if(line[0] == '#') continue;
  490. switch(expected) {
  491. case STATE_FILE_VERSION:
  492. i=atoi(line);
  493. if(i!=NP_STATE_FORMAT_VERSION)
  494. failure++;
  495. else
  496. expected=STATE_DATA_VERSION;
  497. break;
  498. case STATE_DATA_VERSION:
  499. i=atoi(line);
  500. if(i != this_nagios_plugin->state->data_version)
  501. failure++;
  502. else
  503. expected=STATE_DATA_TIME;
  504. break;
  505. case STATE_DATA_TIME:
  506. /* If time > now, error */
  507. data_time=strtoul(line,NULL,10);
  508. if(data_time > current_time)
  509. failure++;
  510. else {
  511. this_nagios_plugin->state->state_data->time = data_time;
  512. expected=STATE_DATA_TEXT;
  513. }
  514. break;
  515. case STATE_DATA_TEXT:
  516. this_nagios_plugin->state->state_data->data = strdup(line);
  517. if(this_nagios_plugin->state->state_data->data==NULL)
  518. die(STATE_UNKNOWN, _("Cannot execute strdup: %s"), strerror(errno));
  519. expected=STATE_DATA_END;
  520. status=TRUE;
  521. break;
  522. case STATE_DATA_END:
  523. ;
  524. }
  525. }
  526. np_free(line);
  527. return status;
  528. }
  529. /*
  530. * If time=NULL, use current time. Create state file, with state format
  531. * version, default text. Writes version, time, and data. Avoid locking
  532. * problems - use mv to write and then swap. Possible loss of state data if
  533. * two things writing to same key at same time.
  534. * Will die with UNKNOWN if errors
  535. */
  536. void np_state_write_string(time_t data_time, char *data_string) {
  537. FILE *fp;
  538. char *temp_file=NULL;
  539. int fd=0, result=0;
  540. time_t current_time;
  541. char *directories=NULL;
  542. char *p=NULL;
  543. if(data_time==0)
  544. time(&current_time);
  545. else
  546. current_time=data_time;
  547. /* If file doesn't currently exist, create directories */
  548. if(access(this_nagios_plugin->state->_filename,F_OK)!=0) {
  549. result = asprintf(&directories, "%s", this_nagios_plugin->state->_filename);
  550. if (result < 0)
  551. die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
  552. if(directories==NULL)
  553. die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
  554. strerror(errno));
  555. for(p=directories+1; *p; p++) {
  556. if(*p=='/') {
  557. *p='\0';
  558. if((access(directories,F_OK)!=0) && (mkdir(directories, S_IRWXU)!=0)) {
  559. /* Can't free this! Otherwise error message is wrong! */
  560. /* np_free(directories); */
  561. die(STATE_UNKNOWN, _("Cannot create directory: %s"), directories);
  562. }
  563. *p='/';
  564. }
  565. }
  566. np_free(directories);
  567. }
  568. result = asprintf(&temp_file,"%s.XXXXXX",this_nagios_plugin->state->_filename);
  569. if (result < 0)
  570. die(STATE_UNKNOWN, "%s %s\n", _("Cannot allocate memory:"), strerror(errno));
  571. if(temp_file==NULL)
  572. die(STATE_UNKNOWN, _("Cannot allocate memory: %s"),
  573. strerror(errno));
  574. if((fd=mkstemp(temp_file))==-1) {
  575. np_free(temp_file);
  576. die(STATE_UNKNOWN, _("Cannot create temporary filename"));
  577. }
  578. fp=(FILE *)fdopen(fd,"w");
  579. if(fp==NULL) {
  580. close(fd);
  581. unlink(temp_file);
  582. np_free(temp_file);
  583. die(STATE_UNKNOWN, _("Unable to open temporary state file"));
  584. }
  585. fprintf(fp,"# NP State file\n");
  586. fprintf(fp,"%d\n",NP_STATE_FORMAT_VERSION);
  587. fprintf(fp,"%d\n",this_nagios_plugin->state->data_version);
  588. fprintf(fp,"%lu\n",current_time);
  589. fprintf(fp,"%s\n",data_string);
  590. fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP);
  591. fflush(fp);
  592. result=fclose(fp);
  593. fsync(fd);
  594. if(result!=0) {
  595. unlink(temp_file);
  596. np_free(temp_file);
  597. die(STATE_UNKNOWN, _("Error writing temp file"));
  598. }
  599. if(rename(temp_file, this_nagios_plugin->state->_filename)!=0) {
  600. unlink(temp_file);
  601. np_free(temp_file);
  602. die(STATE_UNKNOWN, _("Cannot rename state temp file"));
  603. }
  604. np_free(temp_file);
  605. }