<div dir="ltr">Hi,<br>First of all, I'm rather weak in C. So please be gentle. :)<br><br>I have been working on a modification to check_by_ssh that will append a couple of html links to the end of the output. Basically, whenever a check runs I want to get back:<br>
<Check Output><Link 1><Link 2><br><br>The links will go to an internal wiki we use at my organization.<br><br>I got this working fine, or so I thought, a couple of nights ago. However, upon deployment, a number of checks caused my build to fail with a segmentation fault. This would make sense if the output of these failing checks were largely different from the output of the succeeding checks, but they aren't.<br>
<br>It's roughly the same number of characters. The string with the html links is much larger than the output of the checks that are not working. <br><br>Here is the code: (an experienced C programmer is probably going to have a visceral reaction to some of my methods!)<br>
######################################################<br> /* run the command */<br> if (verbose)<br> printf ("%s\n", comm);<br><br> /* my modifications start here*/<br> char* startString = "check"; /* I get the name of the check by searching the output to find this string*/<br>
char* endString = " "; /* and then I look for the next single space */<br> char* startLocation;<br> char* endLocation;<br> char* endLocation2;<br> char* strCheck;<br> char* temp = comm; /* the string I search for the name of the check running *<br>
char* strHost; /* this will hold the name of the host the check is being run on...*/<br> char* strLinks; /*the links...generated below*/<br> int tempLength1;<br> char* tempString;<br> char* holder; /*copy of output to parse through for host name*/<br>
<br> startLocation = strstr(temp,startString);<br> endLocation = strstr(startLocation,endString);<br> tempLength1 = (int)startLocation-(int)endLocation;<br> /* make sure the answer is positive */<br>
if(tempLength1 < 0)<br> tempLength1 = tempLength1*-1;<br><br> strncpy(strCheck,startLocation,tempLength1);<br> strCheck[tempLength1] = '\0'; /*make sure the string is properly terminated*/<br>
/*original nagios code! I modify comm in the proccess_arguments function...as notated later*/<br> result = np_runcmd(comm, &chld_out, &chld_err, 0); <br> holder = strdup(chld_out.buf); /*copy of output*/<br>
<br> strHost = strtok(holder,":"); /*grab host name from output in the form of hostname:output*/<br><br> asprintf(&strLinks, "<a target=\"_blank\" href=\"<a href="https://noctalk.liberty.edu/wiki/%s:%s\">https://noctalk.liberty.edu/wiki/%s:%s\</a>">(Wiki Article)</a> <a target=\"_blank\" href=\"<a href="https://noctalk.liberty.edu/wiki/Special:Search?search=%s&fulltext=Search;\">https://noctalk.liberty.edu/wiki/Special:Search?search=%s&fulltext=Search;\</a>">(Wiki Search)</a>", strCheck, strHost, strCheck); */generate links */<br>
<br> strcat(chld_out.buf, strLinks);<br>#########################################################<br clear="all"><br>And here is where I modify the command being run to get the hostname prepended:<br>#########################################################<br>
process_arguments (int argc, char **argv)<br>{<br> char* cmdHost = "echo -n $HOSTNAME;echo -n :;"; /*echo hostname and a colon */<br><br>............skipping down......<br><br> case 'C': /* Command for remote machine */<br>
commands++;<br> if (commands > 1)<br> asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);<br> asprintf(&remotecmd, "%s%s%s",cmdHost,remotecmd, optarg); /* my
mod */<br>
//asprintf (&remotecmd, "%s%s", remotecmd, optarg); <---Original code<br>#########################################################<br><br>That's it. For the checks that fail, I can change strcat(chld_out.buf, strLinks) to strncat(chld_out.buf, strLinks, 10) and it works...ie, no segmentation fault. However, at 11 characters it fails.<br>
<br>Any ideas? I'm attaching check_by_ssh.c. It doesn't have all of these comments and I removed some lines that were for my testing, but it is basically the same.<br><br>I will be eternally grateful to anyone who can either:<br>
1. Show me what I'm doing wrong.<br>OR<br>2. Help me understand how chld_out.buf works better so I can understand why this might be failing.<br><br>Clifton<br><br>-- <br>"True Christians consider themselves as not satisfying some rigorous creditor, but as discharging a debt of gratitude. Accordingly, theirs is not the stinted return of a constrained obedience, but the large and liberal measure of voluntary service." - William Wilberforce, Real Christianity<br>
</div>