Skip to content
This repository has been archived by the owner on Jan 15, 2019. It is now read-only.

Commit

Permalink
core: revamp the detection of embedded perl usage directive "# icinga…
Browse files Browse the repository at this point in the history
…: +epn" (Andreas Ericsson) #2197

patch is revamping the detection of the first 10 lines containing the
"#icinga: +epn" line.

previously, the core would have read only the first 80 characters from
each line via fgets, which could be to less. the inner handling is
changed to 256 chars, which should be sufficient. removing the strtok on
':' is also a good idea, as this can be done via the strstr pointer
match on 'epn' as well, calculating the return value based on the
character ('+' means embedded perl enabled).

refs #2197
  • Loading branch information
Michael Friedrich committed Jul 6, 2012
1 parent debab14 commit 9707f13
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 53 deletions.
1 change: 1 addition & 0 deletions Changelog
Expand Up @@ -17,6 +17,7 @@ ENHANCEMENTS
FIXES
* core: unify check scheduling replacement logic for new events (Andreas Ericsson) #2676
* core: get rid of the instame macro usage while logging alerts and states (Andreas Ericsson) #2665
* core: revamp the detection of embedded perl usage directive "# icinga: +epn" (Andreas Ericsson) #2197

CHANGES
* icinga.spec: add devel package #2634
Expand Down
89 changes: 36 additions & 53 deletions base/utils.c
Expand Up @@ -3581,72 +3581,55 @@ int deinit_embedded_perl(void) {

/* checks to see if we should run a script using the embedded Perl interpreter */
int file_uses_embedded_perl(char *fname) {
int use_epn = FALSE;
#ifdef EMBEDDEDPERL
#ifndef EMBEDDEDPERL
return FALSE;
#else
int line, use_epn = FALSE;
FILE *fp = NULL;
char line1[80] = "";
char linen[80] = "";
int line = 0;
char *ptr = NULL;
int found_epn_directive = FALSE;

if (enable_embedded_perl == TRUE) {

/* open the file, check if its a Perl script and see if we can use epn */
fp = fopen(fname, "r");
if (fp != NULL) {

/* grab the first line - we should see Perl */
fgets(line1, 80, fp);
char buf[256] = "";

/* yep, its a Perl script... */
if (strstr(line1, "/bin/perl") != NULL) {

/* epn directives must be found in first ten lines of plugin */
for (line = 1; line < 10; line++) {

if (fgets(linen, 80, fp)) {

/* line contains Icinga directives - keep Nagios compatibility */
if (strstr(linen, "# nagios:") || strstr(linen, "# icinga:")) {

ptr = strtok(linen, ":");
if (enable_embedded_perl != TRUE)
return FALSE;

/* process each directive */
for (ptr = strtok(NULL, ","); ptr != NULL; ptr = strtok(NULL, ",")) {

strip(ptr);
/* open the file, check if its a Perl script and see if we can use epn */
fp = fopen(fname, "r");
if (fp == NULL)
return FALSE;

if (!strcmp(ptr, "+epn")) {
use_epn = TRUE;
found_epn_directive = TRUE;
} else if (!strcmp(ptr, "-epn")) {
use_epn = FALSE;
found_epn_directive = TRUE;
}
}
}
/* grab the first line - we should see Perl. go home if not */
if (fgets(line1, 80, fp) == NULL || strstr(buf, "/bin/perl") == NULL) {
fclose(fp);
}

if (found_epn_directive == TRUE)
break;
}
/* epn directives must be found in first ten lines of plugin */
for (line = 1; line < 10; line++) {
if (fgets(buf, sizeof(buf) - 1, fp) == NULL)
break;

/* EOF */
else
break;
}
buf[sizeof(buf) - 1] = '\0';

/* if the plugin didn't tell us whether or not to use embedded Perl, use implicit value */
if (found_epn_directive == FALSE)
use_epn = (use_embedded_perl_implicitly == TRUE) ? TRUE : FALSE;
}
/* line contains Icinga directives - keep Nagios compatibility */
if (strstr(linen, "# nagios:") || strstr(linen, "# icinga:")) {
char *p;
p = strstr(buf + 8, "epn");
if (!p)
continue;

/*
* we found it, so close the file and return
* whatever it shows. '+epn' means yes. everything
* else means no.
*/
fclose(fp);
return *(p - 1) == '+' ? TRUE : FALSE;
}
}
#endif

return use_epn;
fclose(fp);

return use_embedded_perl_implicitly;
#endif
}


Expand Down

0 comments on commit 9707f13

Please sign in to comment.