Voici mon code:
const char *progname = "test_snmp";
const char *revision = "$Revision: 1.0 $";
const char *copyright = "2009-01-23";
const char *email = "exemple@gmail.com";
#include "common.h"
#include "utils.h"
#include "popen.h"
#include <dbi/dbi.h>
#define DEFAULT_COMMUNITY "public"
#define DEFAULT_OID "1.3.6.1.4.1.9.9.156.1.2.1.1.5."
#define DEFAULT_PORT "161"
#define DEFAULT_PROTOCOL "1"
#define DEFAULT_TIMEOUT "3"
#define DEFAULT_RETRIES "0"
#define DEFAULT_DELIMITER "="
#define DEFAULT_OUTPUT_DELIMITER " "
#define MAX_DELIM_LENGTH 8
int process_arguments (int, char **);
void print_usage (void);
char *server_address = NULL;
char *server_port;
char *community = NULL;
char *network_interface = "1";
char *destination = "0";
char *oid;
char *retries;
char *delimiter;
char *output_delim;
char perfstr[MAX_INPUT_BUFFER] = "";
int main (int argc, char **argv)
{
int i = 0;
int result = STATE_UNKNOWN;
int found = 0;
char input_buffer[MAX_INPUT_BUFFER];
char *command_line = NULL;
char *response = NULL;
char *outbuff;
char *output;
char *ptr = NULL;
char *p2 = NULL;
char *show = NULL;
char type[8] = "";
unsigned int istate;
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
/* Configure variables */
outbuff = strdup ("");
output = strdup ("");
delimiter = strdup (" = ");
output_delim = strdup (DEFAULT_OUTPUT_DELIMITER);
server_port = strdup (DEFAULT_PORT);
retries = strdup (DEFAULT_RETRIES);
oid = strdup (DEFAULT_OID);
/* Parse parametres */
if (process_arguments (argc, argv) == ERROR)
usage4 (_("Could not parse arguments"));
/* Genere OID */
strcat(oid, network_interface);
printf("\n%s -t %s -r %s -v 1 -c %s %s %s",PATH_TO_SNMPGET, DEFAULT_TIMEOUT, retries, community, server_address, /*server_port,*/ oid);
/* Genere commande */
asprintf (&command_line, "%s -t %s -r %s -v 1 -c %s %s %s",
PATH_TO_SNMPGET, DEFAULT_TIMEOUT, retries, community, server_address, /*server_port,*/ oid);
/* run the command */
child_process = spopen (command_line);
if (child_process == NULL) {
printf (_("Could not open pipe: %s\n"), command_line);
exit (STATE_UNKNOWN);
}
// Lecture resultat
while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process))
asprintf (&output, "%s%s", output, input_buffer);
ptr = output;
while (ptr)
{
char *foo, *ptr2;
unsigned int copylen;
foo = strstr (ptr, delimiter);
copylen = foo-ptr;
if (copylen > sizeof(perfstr)-strlen(perfstr)-1)
copylen = sizeof(perfstr)-strlen(perfstr)-1;
ptr2 = ptr;
ptr = foo;
if (ptr == NULL)
break;
ptr += strlen (delimiter);
ptr += strspn (ptr, " ");
found++;
if (ptr[0] == '"')
{
ptr++;
response = strpcpy (response, ptr, "\"");
ptr = strpbrk (ptr, "\"");
ptr += strspn (ptr, "\"\n");
}
else {
response = strpcpy (response, ptr, "\n");
ptr = strpbrk (ptr, "\n");
ptr += strspn (ptr, "\n");
while (strstr (ptr, delimiter) && strstr (ptr, "\n") && strstr (ptr, "\n") < strstr (ptr, delimiter))
{
response = strpcat (response, ptr, "\n");
ptr = strpbrk (ptr, "\n");
}
if (ptr && strstr (ptr, delimiter) == NULL)
{
asprintf (&response, "%s%s", response, ptr);
ptr = NULL;
}
}
/* We strip out the datatype indicator for PHBs */
/* Clean up type array - Sol10 does not necessarily zero it out */
bzero(type, sizeof(type));
if (strstr (response, "Gauge: "))
show = strstr (response, "Gauge: ") + 7;
else if (strstr (response, "Gauge32: "))
show = strstr (response, "Gauge32: ") + 9;
else if (strstr (response, "Counter32: "))
{
show = strstr (response, "Counter32: ") + 11;
strcpy(type, "c");
}
else if (strstr (response, "Counter64: "))
{
show = strstr (response, "Counter64: ") + 11;
strcpy(type, "c");
}
else if (strstr (response, "INTEGER: "))
show = strstr (response, "INTEGER: ") + 9;
else if (strstr (response, "STRING: "))
show = strstr (response, "STRING: ") + 8;
else
show = response;
p2 = show;
/* Analyse resultat et update db */
printf("\nName: %s", p2);
/* fin analyse */
} /* end while (ptr) */
if (found == 0)
{
die (STATE_UNKNOWN, "No data received from host\n");
}
/* close the pipe */
if (spclose (child_process))
{
if (result == STATE_OK)
result = STATE_UNKNOWN;
asprintf (&outbuff, "%s (%s)", outbuff, "snmpget returned an error status");
}
printf ("%s\n", outbuff);
return result;
}
/* process command-line arguments */
int process_arguments (int argc, char **argv)
{
char *ptr;
int c = 1;
int j = 0, jj = 0, ii = 0;
int option = 0;
static struct option longopts[] = {
STD_LONG_OPTS,
{"community", required_argument, 0, 'C'},
{"interface", required_argument, 0, 'I'},
{"destination", required_argument, 0, 'D'},
{0, 0, 0, 0}
};
if (argc < 2)
return ERROR;
while (1)
{
c = getopt_long (argc, argv, "C:H:P:R:I:D:", longopts, &option);
if (c == -1 || c == EOF)
break;
switch (c)
{
case 'C': /* group or community */
community = optarg;
break;
case 'H': /* Host or server */
server_address = optarg;
break;
case 'P': /* Port */
server_port = optarg;
break;
case 'R': /* Retry */
retries = optarg;
break;
case 'I': /* interface */
network_interface = optarg;
break;
case 'D': /* destination */
destination = optarg;
break;
}
}
return OK;
}
void print_usage (void)
{
printf (_("Usage:"));
printf ("%s -H <ip_address>\n", progname);
printf ("-C <community>\n");
printf ("-I <interface>\n");
printf ("-D <destination>\n");
printf ("[-P <server_port>]\n");
printf ("[-R <retry count>]\n");
}
les 2 OS sont Ubuntu...
Je ne comprends tjs pas...
en ligne de commande via : snmpget -t 3 -r 0 -v 1 -c nomCommunaute ip 1.3.6.1.4.1.9.9.156.1.2.1.1.5.1
ça fonctionne dans les 2 OS!!!
Je ne comprends tjs pas pq le programme me lance un segmentation fault avant de rentrer dans le main...
Si quelqu'un a une idée?