~ubuntu-branches/ubuntu/saucy/nut/saucy

« back to all changes in this revision

Viewing changes to drivers/snmp-ups.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2005-07-20 19:48:50 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20050720194850-oo61wjr33rrx2mre
Tags: upstream-2.0.2
ImportĀ upstreamĀ versionĀ 2.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 *
3
3
 *  Based on NetSNMP API (Simple Network Management Protocol V1-2)
4
4
 *
5
 
 *  Copyright (C) 2002-2003 
 
5
 *  Copyright (C) 2002-2004 
6
6
 *                      Arnaud Quette <arnaud.quette@free.fr>
7
7
 *                      Dmitry Frolov <frolov@riss-telecom.ru>
8
8
 *                      J.W. Hoogervorst <jeroen@hoogervorst.net>
 
9
 *                      Niels Baggesen <niels@baggesen.net>
9
10
 *
10
 
 *  Sponsored by MGE UPS SYSTEMS <http://www.mgeups.com>
 
11
 *  Sponsored by MGE UPS SYSTEMS <http://opensource.mgeups.com/>
11
12
 *
12
13
 *  This program is free software; you can redistribute it and/or modify
13
14
 *  it under the terms of the GNU General Public License as published by
35
36
#include "apccmib.h"
36
37
#include "ietfmib.h"
37
38
#include "mgemib.h"
 
39
#include "netvisionmib.h"
 
40
#include "pwmib.h"
38
41
 
39
42
/* pointer to the Snmp2Nut lookup table */
40
43
snmp_info_t *snmp_info;
58
61
                        dstate_addcmd(su_info_p->info_type);
59
62
 
60
63
        /* setup handlers for instcmd and setvar functions */
61
 
        upsh.new_setvar = su_setvar;
62
 
        upsh.new_instcmd = su_instcmd;
 
64
        upsh.setvar = su_setvar;
 
65
        upsh.instcmd = su_instcmd;
 
66
 
 
67
        if (testvar("notransferoids"))
 
68
                disable_transfer_oids();
63
69
 
64
70
        /* initialize all other INFO_ fields from list */
65
71
        if (snmp_ups_walk(SU_WALKMODE_INIT))
107
113
{
108
114
  upsdebugx(1, "entering upsdrv_makevartable()");
109
115
 
110
 
  addvar(VAR_VALUE, SU_VAR_MIBS, "Set MIB compliance (default=ietf, allowed mge,apcc)");
 
116
  addvar(VAR_VALUE, SU_VAR_MIBS, "Set MIB compliance (default=ietf, allowed mge,apcc,netvision,pw)");
111
117
  addvar(VAR_VALUE | VAR_SENSITIVE, SU_VAR_COMMUNITY, "Set community name (default=public)");
112
118
  addvar(VAR_VALUE, SU_VAR_VERSION, "Set SNMP version (default=v1, allowed v2c)");
113
119
  addvar(VAR_VALUE, SU_VAR_POLLFREQ, "Set polling frequency in seconds, to reduce network flow (default=30)");
 
120
  addvar(VAR_FLAG, "notransferoids", "Disable transfer OIDs (use on APCC Symmetras)");
114
121
}
115
122
 
116
123
void upsdrv_banner(void)
174
181
        /* Initialize session */
175
182
        snmp_sess_init(&g_snmp_sess);
176
183
 
177
 
        g_snmp_sess.peername = (char *)hostname;
178
 
        g_snmp_sess.community = (char *)community;
 
184
        g_snmp_sess.peername = xstrdup(hostname);
 
185
        g_snmp_sess.community = xstrdup(community);
179
186
        g_snmp_sess.community_len = strlen(community);
180
187
        g_snmp_sess.version = SNMP_VERSION_1;
181
188
 
184
191
        g_snmp_sess_p = snmp_open(&g_snmp_sess);        /* establish the session */
185
192
        if (g_snmp_sess_p == NULL) {
186
193
                nut_snmp_perror(&g_snmp_sess, 0, NULL, "nut_snmp_init: snmp_open");
187
 
                exit(1);
 
194
                exit(EXIT_FAILURE);
188
195
        }
189
196
}
190
197
 
200
207
 
201
208
static int oid_compare(const char *oid_req_in, long *oid_ans, size_t oid_len)
202
209
{
203
 
        int     ctr;
 
210
        unsigned int    ctr;
204
211
        long    val;
205
212
        char    *tmp, *ptr, *oid_req;
206
213
 
480
487
                                upsname, buf, snmp_errstring(response->errstat));
481
488
        } else if (status == STAT_TIMEOUT) {
482
489
                upslogx(LOG_ERR, "[%s] %s: Timeout: no response from %s",
483
 
                        upsname, sess->peername);
 
490
                        upsname, buf, sess->peername);
484
491
        } else {
485
492
                snmp_sess_error(sess, &cliberr, &snmperr, &snmperrstr);
486
493
                upslogx(LOG_ERR, "[%s] %s: %s",
493
500
 * utility functions.
494
501
 * ----------------------------------------------------------- */
495
502
 
 
503
/* deal with APCC weirdness on Symmetras */
 
504
static void disable_transfer_oids(void)
 
505
{
 
506
        snmp_info_t *su_info_p;
 
507
 
 
508
        upslogx(LOG_INFO, "Disabling transfer OIDs");
 
509
 
 
510
        for (su_info_p = &snmp_info[0]; su_info_p->info_type != NULL ; su_info_p++) {
 
511
                if (!strcasecmp(su_info_p->info_type, "input.transfer.low")) {
 
512
                        su_info_p->flags &= ~SU_FLAG_OK;
 
513
                        continue;
 
514
                }
 
515
 
 
516
                if (!strcasecmp(su_info_p->info_type, "input.transfer.high")) {
 
517
                        su_info_p->flags &= ~SU_FLAG_OK;
 
518
                        continue;
 
519
                }
 
520
        }
 
521
}
 
522
 
496
523
/* universal function to add or update info element. */
497
524
void su_setinfo(const char *type, const char *value, int flags, int auxdata)
498
525
{
514
541
 
515
542
void su_status_set(snmp_info_t *su_info_p, long value)
516
543
{       
517
 
        char *info_value = NULL;
 
544
        const char *info_value = NULL;
518
545
 
519
546
        upsdebugx(2, "SNMP UPS driver : entering su_status_set()");
520
547
 
538
565
                if (!strcasecmp(su_info_p->info_type, type))
539
566
                        return su_info_p;
540
567
                
541
 
        fatalx("nut_snmp_find_info: unknown info type: 0x%x", type);
 
568
        fatalx("nut_snmp_find_info: unknown info type: %s", type);
542
569
        return NULL;
543
570
}
544
571
 
545
572
/* Load the right snmp_info_t structure matching mib parameter */
546
 
void load_mib2nut(char *mib)
 
573
void load_mib2nut(const char *mib)
547
574
{
548
575
        upsdebugx(2, "SNMP UPS driver : entering load_mib2nut(%s)", mib);
549
576
        
558
585
                snmp_info = &mge_mib[0];
559
586
                OID_pwr_status = "";
560
587
/*              read_mibconf("mgemib"); */
 
588
        } else if (!strcmp(mib, "netvision")) {
 
589
                upsdebugx(1, "load_mib2nut: using netvision (SOCOMEC SICON UPS) mib");
 
590
                snmp_info = &netvision_mib[0];
 
591
                OID_pwr_status = "";
 
592
/*             read_mibconf("netvisionmib"); */
 
593
        } else if (!strcmp(mib, "pw")) {
 
594
                upsdebugx(1, "load_mib2nut: using using pw (Powerware) mib");
 
595
                snmp_info = &pw_mib[0];
 
596
                OID_pwr_status = "";
 
597
/*             read_mibconf("pw"); */
561
598
        } else {
562
599
                upsdebugx(1, "load_mib2nut: using ietf (RFC 1628) mib");
563
600
                snmp_info = &ietf_mib[0];
575
612
                (strcmp(info_lkp->info_value, "NULL")); info_lkp++) {
576
613
                        
577
614
                if (!(strcmp(info_lkp->info_value, value))) {
578
 
                        upsdebugx(1, "su_find_valinfo: found %s (value: %d)",
 
615
                        upsdebugx(1, "su_find_valinfo: found %s (value: %s)",
579
616
                                        info_lkp->info_value, value);
580
617
                        
581
618
                        return info_lkp->oid_value;
582
619
                }
583
620
        }
584
 
        upsdebugx(1, "su_find_infoval: no matching INFO_* value for this OID value (%d)", value);
 
621
        upsdebugx(1, "su_find_infoval: no matching INFO_* value for this OID value (%s)", value);
585
622
        return -1;
586
623
}
587
624
 
588
625
/* find the INFO_* value matching that OID value */
589
 
char *su_find_infoval(info_lkp_t *oid2info, long value)
 
626
const char *su_find_infoval(info_lkp_t *oid2info, long value)
590
627
{
591
628
        info_lkp_t *info_lkp;
592
629
                
594
631
                (strcmp(info_lkp->info_value, "NULL")); info_lkp++) {
595
632
                        
596
633
                if (info_lkp->oid_value == value) {
597
 
                        upsdebugx(1, "su_find_infoval: found %s (value: %d)",
 
634
                        upsdebugx(1, "su_find_infoval: found %s (value: %ld)",
598
635
                                        info_lkp->info_value, value);
599
636
                        
600
637
                        return info_lkp->info_value;
601
638
                }
602
639
        }
603
 
        upsdebugx(1, "su_find_infoval: no matching INFO_* value for this OID value (%d)", value);
 
640
        upsdebugx(1, "su_find_infoval: no matching INFO_* value for this OID value (%ld)", value);
604
641
        return NULL;
605
642
}
606
643
 
691
728
                if (status == TRUE)
692
729
                {
693
730
                        su_status_set(su_info_p, value);
694
 
                        upsdebugx(2, "=> value: %d", value);
 
731
                        upsdebugx(2, "=> value: %ld", value);
695
732
                }
696
733
 
697
734
                return status;
755
792
                return STAT_SET_UNKNOWN;
756
793
        }
757
794
 
758
 
        if (!(su_info_p->info_flags & FLAG_RW) || su_info_p->OID == NULL) {
 
795
        if (!(su_info_p->info_flags & ST_FLAG_RW) || su_info_p->OID == NULL) {
759
796
                upslogx(LOG_ERR, "su_setvar: not writable %s", varname);
760
797
                return STAT_SET_UNKNOWN; /* STAT_SET_UNHANDLED would be better */
761
798
        }
797
834
        }
798
835
 
799
836
        /* set value. */
800
 
        if (su_info_p->info_flags & FLAG_STRING) {
 
837
        if (su_info_p->info_flags & ST_FLAG_STRING) {
801
838
                status = nut_snmp_set_str(su_info_p->OID, su_info_p->dfl);
802
839
        } else {
803
840
                status = nut_snmp_set_int(su_info_p->OID, su_info_p->info_len);
894
931
        return 1;
895
932
}
896
933
/* called for fatal errors in parseconf like malloc failures */
897
 
void mibconf_err(const char *errmsg)
 
934
static void mibconf_err(const char *errmsg)
898
935
{
899
936
        upslogx(LOG_ERR, "Fatal error in parseconf (*mib.conf): %s", errmsg);
900
937
}
924
961
                        continue;
925
962
 
926
963
                if (!parse_mibconf_args(ctx.numargs, ctx.arglist)) {
927
 
                        int     i;
 
964
                        unsigned int    i;
928
965
                        char    errmsg[SMALLBUF];
929
966
 
930
967
                        snprintf(errmsg, sizeof(errmsg), 
934
971
                                snprintfcat(errmsg, sizeof(errmsg), " %s", 
935
972
                                        ctx.arglist[i]);
936
973
 
937
 
                        upslogx(LOG_WARNING, errmsg);
 
974
                        upslogx(LOG_WARNING, "%s", errmsg);
938
975
                }
939
976
        }
940
977
        pconf_finish(&ctx);