summaryrefslogtreecommitdiffstats
path: root/src/alphacode/poller
diff options
context:
space:
mode:
authorhugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2006-02-28 10:34:47 +0000
committerhugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082>2006-02-28 10:34:47 +0000
commitf5d7aef02b4a70eab0c650969fbfb49c565a4fa8 (patch)
tree91b9f4916c458b87ef52fd70cd07eff5796816d1 /src/alphacode/poller
parentb2b44cfdb92e70cc32d08443660139905346c943 (diff)
downloadzabbix-f5d7aef02b4a70eab0c650969fbfb49c565a4fa8.tar.gz
zabbix-f5d7aef02b4a70eab0c650969fbfb49c565a4fa8.tar.xz
zabbix-f5d7aef02b4a70eab0c650969fbfb49c565a4fa8.zip
Minor changes
git-svn-id: svn://svn.zabbix.com/trunk@2693 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src/alphacode/poller')
-rwxr-xr-xsrc/alphacode/poller/snmp.c287
1 files changed, 124 insertions, 163 deletions
diff --git a/src/alphacode/poller/snmp.c b/src/alphacode/poller/snmp.c
index dfd8f5a9..a8c7293c 100755
--- a/src/alphacode/poller/snmp.c
+++ b/src/alphacode/poller/snmp.c
@@ -135,19 +135,19 @@ struct oid {
*/
void initialize (void)
{
- struct oid *op = oids;
+ struct oid *op = oids;
- init_snmp("asynchapp");
+ init_snmp("asynchapp");
- /* parse the oids */
- while (op->Name) {
- op->OidLen = sizeof(op->Oid)/sizeof(op->Oid[0]);
- if (!read_objid(op->Name, op->Oid, &op->OidLen)) {
- snmp_perror("read_objid");
- exit(1);
- }
- op++;
- }
+/* parse the oids */
+ while (op->Name) {
+ op->OidLen = sizeof(op->Oid)/sizeof(op->Oid[0]);
+ if (!read_objid(op->Name, op->Oid, &op->OidLen)) {
+ snmp_perror("read_objid");
+ exit(1);
+ }
+ op++;
+ }
}
/*
@@ -155,80 +155,44 @@ void initialize (void)
*/
int print_result (int status, struct snmp_session *sp, struct snmp_pdu *pdu)
{
- char buf[1024];
- struct variable_list *vp;
- int ix;
- struct timeval now;
- struct timezone tz;
- struct tm *tm;
-
- gettimeofday(&now, &tz);
- tm = localtime(&now.tv_sec);
- fprintf(stdout, "%.2d:%.2d:%.2d.%.6d ", tm->tm_hour, tm->tm_min, tm->tm_sec,
- now.tv_usec);
- switch (status) {
- case STAT_SUCCESS:
- vp = pdu->variables;
- if (pdu->errstat == SNMP_ERR_NOERROR) {
- while (vp) {
- snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp);
- fprintf(stdout, "%s: %s\n", sp->peername, buf);
- vp = vp->next_variable;
- }
- }
- else {
- for (ix = 1; vp && ix != pdu->errindex; vp = vp->next_variable, ix++)
- ;
- if (vp) snprint_objid(buf, sizeof(buf), vp->name, vp->name_length);
- else strcpy(buf, "(none)");
- fprintf(stdout, "%s: %s: %s\n",
- sp->peername, buf, snmp_errstring(pdu->errstat));
- }
- return 1;
- case STAT_TIMEOUT:
- fprintf(stdout, "%s: Timeout\n", sp->peername);
- return 0;
- case STAT_ERROR:
- snmp_perror(sp->peername);
- return 0;
- }
- return 0;
-}
-
-/*****************************************************************************/
+ char buf[1024];
+ struct variable_list *vp;
+ int ix;
+ struct timeval now;
+ struct timezone tz;
+ struct tm *tm;
-/*
- * simple synchronous loop
- */
-void synchronous (void)
-{
- struct host *hp;
-
- for (hp = hosts; hp->name; hp++) {
- struct snmp_session ss, *sp;
- struct oid *op;
-
- snmp_sess_init(&ss); /* initialize session */
- ss.version = SNMP_VERSION_1;
-// ss.version = SNMP_VERSION_2c;
- ss.peername = strdup(hp->name);
- ss.community = strdup(hp->community);
- ss.community_len = strlen(ss.community);
- if (!(sp = snmp_open(&ss))) {
- snmp_perror("snmp_open");
- continue;
- }
- for (op = oids; op->Name; op++) {
- struct snmp_pdu *req, *resp;
- int status;
- req = snmp_pdu_create(SNMP_MSG_GET);
- snmp_add_null_var(req, op->Oid, op->OidLen);
- status = snmp_synch_response(sp, req, &resp);
- if (!print_result(status, sp, resp)) break;
- snmp_free_pdu(resp);
- }
- snmp_close(sp);
- }
+ gettimeofday(&now, &tz);
+ tm = localtime(&now.tv_sec);
+ fprintf(stdout, "%.2d:%.2d:%.2d.%.6d ", tm->tm_hour, tm->tm_min, tm->tm_sec,
+ now.tv_usec);
+ switch (status) {
+ case STAT_SUCCESS:
+ vp = pdu->variables;
+ if (pdu->errstat == SNMP_ERR_NOERROR) {
+ while (vp) {
+ snprint_variable(buf, sizeof(buf), vp->name, vp->name_length, vp);
+ fprintf(stdout, "%s: %s\n", sp->peername, buf);
+ vp = vp->next_variable;
+ }
+ }
+ else {
+ for (ix = 1; vp && ix != pdu->errindex; vp = vp->next_variable, ix++)
+ ;
+ if (vp) snprint_objid(buf, sizeof(buf), vp->name, vp->name_length);
+ else strcpy(buf, "(none)");
+ fprintf(stdout, "%s: %s: %s\n",
+ sp->peername, buf, snmp_errstring(pdu->errstat));
+ }
+ return 1;
+ case STAT_TIMEOUT:
+ fprintf(stdout, "%s: Timeout\n", sp->peername);
+ return 0;
+ case STAT_ERROR:
+ snmp_perror(sp->peername);
+ return 0;
+ }
+ return 0;
}
/*****************************************************************************/
@@ -249,105 +213,102 @@ int active_hosts; /* hosts that we have not completed */
int asynch_response(int operation, struct snmp_session *sp, int reqid,
struct snmp_pdu *pdu, void *magic)
{
- struct session *host = (struct session *)magic;
- struct snmp_pdu *req;
+ struct session *host = (struct session *)magic;
+ struct snmp_pdu *req;
- if (operation == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) {
- if (print_result(STAT_SUCCESS, host->sess, pdu)) {
- host->current_oid++; /* send next GET (if any) */
- if (host->current_oid->Name) {
- req = snmp_pdu_create(SNMP_MSG_GET);
- snmp_add_null_var(req, host->current_oid->Oid, host->current_oid->OidLen);
- if (snmp_send(host->sess, req))
- return 1;
- else {
- snmp_perror("snmp_send");
- snmp_free_pdu(req);
+ if (operation == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) {
+ if (print_result(STAT_SUCCESS, host->sess, pdu)) {
+ host->current_oid++; /* send next GET (if any) */
+ if (host->current_oid->Name) {
+ req = snmp_pdu_create(SNMP_MSG_GET);
+ snmp_add_null_var(req, host->current_oid->Oid, host->current_oid->OidLen);
+ if (snmp_send(host->sess, req))
+ return 1;
+ else {
+ snmp_perror("snmp_send");
+ snmp_free_pdu(req);
+ }
+ }
+ }
}
- }
- }
- }
- else
- print_result(STAT_TIMEOUT, host->sess, pdu);
+ else
+ print_result(STAT_TIMEOUT, host->sess, pdu);
- /* something went wrong (or end of variables)
- * this host not active any more
- */
- active_hosts--;
- return 1;
+/* something went wrong (or end of variables)
+* this host not active any more
+*/
+ active_hosts--;
+ return 1;
}
void asynchronous(void)
{
- struct session *hs;
- struct host *hp;
+ struct session *hs;
+ struct host *hp;
- /* startup all hosts */
+/* startup all hosts */
- for (hs = sessions, hp = hosts; hp->name; hs++, hp++) {
- struct snmp_pdu *req;
- struct snmp_session sess;
- snmp_sess_init(&sess); /* initialize session */
- sess.version = SNMP_VERSION_1;
-// sess.version = SNMP_VERSION_2c;
- sess.peername = strdup(hp->name);
- sess.community = strdup(hp->community);
- sess.community_len = strlen(sess.community);
- sess.callback = asynch_response; /* default callback */
- sess.callback_magic = hs;
- if (!(hs->sess = snmp_open(&sess))) {
- snmp_perror("snmp_open");
- continue;
- }
- hs->current_oid = oids;
- req = snmp_pdu_create(SNMP_MSG_GET); /* send the first GET */
- snmp_add_null_var(req, hs->current_oid->Oid, hs->current_oid->OidLen);
- if (snmp_send(hs->sess, req))
- active_hosts++;
- else {
- snmp_perror("snmp_send");
- snmp_free_pdu(req);
- }
- }
+ for (hs = sessions, hp = hosts; hp->name; hs++, hp++) {
+ struct snmp_pdu *req;
+ struct snmp_session sess;
+ snmp_sess_init(&sess); /* initialize session */
+ sess.version = SNMP_VERSION_1;
+ // sess.version = SNMP_VERSION_2c;
+ sess.peername = strdup(hp->name);
+ sess.community = strdup(hp->community);
+ sess.community_len = strlen(sess.community);
+ sess.callback = asynch_response; /* default callback */
+ sess.callback_magic = hs;
+ if (!(hs->sess = snmp_open(&sess))) {
+ snmp_perror("snmp_open");
+ continue;
+ }
+ hs->current_oid = oids;
+ req = snmp_pdu_create(SNMP_MSG_GET); /* send the first GET */
+ snmp_add_null_var(req, hs->current_oid->Oid, hs->current_oid->OidLen);
+ if (snmp_send(hs->sess, req))
+ active_hosts++;
+ else {
+ snmp_perror("snmp_send");
+ snmp_free_pdu(req);
+ }
+ }
- /* loop while any active hosts */
+/* loop while any active hosts */
- while (active_hosts) {
- int fds = 0, block = 1;
- fd_set fdset;
- struct timeval timeout;
+ while (active_hosts) {
+ int fds = 0, block = 1;
+ fd_set fdset;
+ struct timeval timeout;
- FD_ZERO(&fdset);
- snmp_select_info(&fds, &fdset, &timeout, &block);
- fds = select(fds, &fdset, NULL, NULL, block ? NULL : &timeout);
- if (fds < 0) {
- perror("select failed");
- exit(1);
- }
- if (fds)
- snmp_read(&fdset);
- else
- snmp_timeout();
- }
+ FD_ZERO(&fdset);
+ snmp_select_info(&fds, &fdset, &timeout, &block);
+ fds = select(fds, &fdset, NULL, NULL, block ? NULL : &timeout);
+ if (fds < 0) {
+ perror("select failed");
+ exit(1);
+ }
+ if (fds)
+ snmp_read(&fdset);
+ else
+ snmp_timeout();
+ }
- /* cleanup */
+/* cleanup */
- for (hp = hosts, hs = sessions; hp->name; hs++, hp++) {
- if (hs->sess) snmp_close(hs->sess);
- }
+ for (hp = hosts, hs = sessions; hp->name; hs++, hp++) {
+ if (hs->sess) snmp_close(hs->sess);
+ }
}
/*****************************************************************************/
int main (int argc, char **argv)
{
- initialize();
-
- printf("---------- synchronous -----------\n");
- synchronous();
+ initialize();
- printf("---------- asynchronous -----------\n");
- asynchronous();
+ printf("---------- asynchronous -----------\n");
+ asynchronous();
- return 0;
+ return 0;
}