check_by_ssh frustration!

Steven D. Morrey smorrey at ldschurch.org
Thu Aug 14 18:44:54 CEST 2008


Just off the top of my head, I believe startlocation and endlocation should be int not char* same with all the variables intended to hold numerical values.


From: nagios-devel-bounces at lists.sourceforge.net [mailto:nagios-devel-bounces at lists.sourceforge.net] On Behalf Of Clifton Griffin
Sent: Thursday, August 14, 2008 10:41 AM
To: nagios-devel at lists.sourceforge.net
Subject: [Nagios-devel] check_by_ssh frustration!

Hi,
First of all, I'm rather weak in C. So please be gentle. :)

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:
<Check Output><Link 1><Link 2>

The links will go to an internal wiki we use at my organization.

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.

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.

Here is the code: (an experienced C programmer is probably going to have a visceral reaction to some of my methods!)
######################################################
        /* run the command */
        if (verbose)
                printf ("%s\n", comm);

       /* my modifications start here*/
        char* startString = "check";        /* I get the name of the check by searching the output to find this string*/
        char* endString = " ";        /* and then I look for the next single space */
        char* startLocation;
        char* endLocation;
        char* endLocation2;
        char* strCheck;
        char* temp = comm; /* the string I search for the name of the check running *
        char* strHost;        /* this will hold the name of the host the check is being run on...*/
        char* strLinks;  /*the links...generated below*/
        int tempLength1;
        char* tempString;
        char* holder; /*copy of output to parse through for host name*/

        startLocation = strstr(temp,startString);
        endLocation = strstr(startLocation,endString);
        tempLength1 = (int)startLocation-(int)endLocation;
        /* make sure the answer is positive */
        if(tempLength1 < 0)
                tempLength1 = tempLength1*-1;

        strncpy(strCheck,startLocation,tempLength1);
        strCheck[tempLength1] = '\0'; /*make sure the string is properly terminated*/
        /*original nagios code! I modify comm in the proccess_arguments function...as notated later*/
        result = np_runcmd(comm, &chld_out, &chld_err, 0);
        holder = strdup(chld_out.buf); /*copy of output*/

        strHost = strtok(holder,":"); /*grab host name from output in the form of hostname:output*/

        asprintf(&strLinks, "<a target=\"_blank\" href=\"https://noctalk.liberty.edu/wiki/%s:%s\<https://noctalk.liberty.edu/wiki/%25s:%25s/>">(Wiki Article)</a> <a target=\"_blank\" href=\"https://noctalk.liberty.edu/wiki/Special:Search?search=%s&fulltext=Search;\<https://noctalk.liberty.edu/wiki/Special:Search?search=%25s&fulltext=Search;\>">(Wiki Search)</a>", strCheck, strHost, strCheck); */generate links */

        strcat(chld_out.buf, strLinks);
#########################################################

And here is where I modify the command being run to get the hostname prepended:
#########################################################
process_arguments (int argc, char **argv)
{
        char* cmdHost = "echo -n $HOSTNAME;echo -n :;"; /*echo hostname and a colon */

............skipping down......

                case 'C':                                                                       /* Command for remote machine */
                        commands++;
                        if (commands > 1)
                                asprintf (&remotecmd, "%s;echo STATUS CODE: $?;", remotecmd);
                        asprintf(&remotecmd, "%s%s%s",cmdHost,remotecmd, optarg); /* my mod */
                        //asprintf (&remotecmd, "%s%s", remotecmd, optarg); <---Original code
#########################################################

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.

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.

I will be eternally grateful to anyone who can either:
1. Show me what I'm doing wrong.
OR
2. Help me understand how chld_out.buf works better so I can understand why this might be failing.

Clifton

--
"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

----------------------------------------------------------------------
NOTICE: This email message is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.monitoring-lists.org/archive/developers/attachments/20080814/d3ba2fb0/attachment.html>
-------------- next part --------------
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
-------------- next part --------------
_______________________________________________
Nagios-devel mailing list
Nagios-devel at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-devel


More information about the Developers mailing list