4
0

utils_base.c 18 KB

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