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

« back to all changes in this revision

Viewing changes to drivers/snmp-ups.h

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Quette
  • Date: 2004-05-28 13:10:01 UTC
  • mto: (16.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20040528131001-yj2m9qcez4ya2w14
Tags: upstream-1.4.2
ImportĀ upstreamĀ versionĀ 1.4.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  snmp-ups.h - NUT Meta SNMP driver (support different MIBS)
 
2
 *
 
3
 *  Based on UCD/NetSNMP API (Simple Network Management Protocol V1-2)
 
4
 *
 
5
 *  Copyright (C) 2002-2003 
 
6
 *                      Arnaud Quette <arnaud.quette@free.fr>
 
7
 *                      Dmitry Frolov <frolov@riss-telecom.ru>
 
8
 *                      J.W. Hoogervorst <jeroen@hoogervorst.net>
 
9
 *
 
10
 *  Sponsored by MGE UPS SYSTEMS <http://www.mgeups.com/opensource/>
 
11
 *
 
12
 *  This program is free software; you can redistribute it and/or modify
 
13
 *  it under the terms of the GNU General Public License as published by
 
14
 *  the Free Software Foundation; either version 2 of the License, or
 
15
 *  (at your option) any later version.
 
16
 *
 
17
 *  This program is distributed in the hope that it will be useful,
 
18
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 *  GNU General Public License for more details.
 
21
 *
 
22
 *  You should have received a copy of the GNU General Public License
 
23
 *  along with this program; if not, write to the Free Software
 
24
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
25
 *
 
26
 */
 
27
 
 
28
/* TODO list:
 
29
- complete shutdown
 
30
- add enum values to OIDs.
 
31
- optimize network flow by constructing one big packet (calling snmp_add_null_var
 
32
for each OID request we made), instead of sending many small packets
 
33
- add support for registration and traps (needed ?),
 
34
- complete mib2nut data (add all OID translation to NUT)
 
35
- externalize mib2nut data in .m2n files and load at driver startup using parseconf()...
 
36
- ... and use Net-SNMP lookup mecanism for OIDs (use string path, not numeric)
 
37
- add mib autodetection support (=> -x mibs=auto),
 
38
- adjust information logging.
 
39
*/
 
40
 
 
41
#include <unistd.h>
 
42
#include <stdio.h>
 
43
#include <stdlib.h>
 
44
#include <string.h>
 
45
#include <net-snmp/net-snmp-config.h>
 
46
#include <net-snmp/net-snmp-includes.h>
 
47
 
 
48
#define DRIVER_VERSION          "0.40"
 
49
 
 
50
#define DEFAULT_POLLFREQ        30              /* in seconds */
 
51
 
 
52
/* use explicit booleans */
 
53
#ifndef FALSE
 
54
typedef enum ebool { FALSE, TRUE } bool;
 
55
#else
 
56
typedef int bool;
 
57
#endif
 
58
 
 
59
/* Common SNMP data and lookup definitions */
 
60
/* special functions to interpret items: 
 
61
   take UPS answer, return string to set in INFO, max len  
 
62
   
 
63
   NOTE: FFE means For Future Extensions
 
64
   
 
65
   */
 
66
   
 
67
/* typedef void (*interpreter)(char *, char *, int); */
 
68
 
 
69
/* for lookup between OID values and INFO_ value */
 
70
typedef struct {
 
71
        int oid_value;          /* OID value */
 
72
        char *info_value;       /* INFO_* value */
 
73
} info_lkp_t;
 
74
 
 
75
/* Structure containing info about one item that can be requested
 
76
   from UPS and set in INFO.  If no interpreter functions is defined,
 
77
   use sprintf with given format string.  If unit is not NONE, values
 
78
   are converted according to the multiplier table  
 
79
*/
 
80
typedef struct {
 
81
        char    *info_type;             /* INFO_ or CMD_ element */
 
82
        int             info_flags;             /* flags to set in addinfo */
 
83
        float   info_len;               /* length of strings if STR, */
 
84
                                                        /* cmd value if CMD, multiplier otherwise. */
 
85
        char    *OID;                   /* SNMP OID or NULL */
 
86
        char    *dfl;                   /* default value */
 
87
        unsigned long flags;    /* my flags */
 
88
        info_lkp_t *oid2info;   /* lookup table between OID and NUT values */
 
89
/*      char *info_OID_format;          *//* FFE: OID format for complex values */
 
90
/*      interpreter interpret;          *//* FFE: interpreter fct, NULL if not needed  */
 
91
        void *next;                     /* next snmp_info_t */
 
92
} snmp_info_t;
 
93
 
 
94
#define SU_FLAG_OK                      (1 << 0)                /* show element to upsd. */
 
95
#define SU_FLAG_STATIC          (1 << 1)                /* retrieve info only once. */
 
96
#define SU_FLAG_ABSENT          (1 << 2)                /* data is absent in the device, */
 
97
                                                                                /* use default value. */
 
98
#define SU_FLAG_STALE           (1 << 3)                /* data stale, don't try too often. */
 
99
 
 
100
/* status string components */
 
101
#define SU_STATUS_PWR           (0 << 8)                /* indicates power status element */
 
102
#define SU_STATUS_BATT          (1 << 8)                /* indicates battery status element */
 
103
#define SU_STATUS_CAL           (2 << 8)                /* indicates calibration status element */
 
104
#define SU_STATUS_RB            (3 << 8)                /* indicates replace battery status element */
 
105
#define SU_STATUS_NUM_ELEM      4
 
106
#define SU_STATUS_INDEX(t)      (((t) >> 8) & 7)
 
107
 
 
108
/* hints for su_ups_set, applicable only to rw vars */
 
109
#define SU_TYPE_INT                     (0 << 16)       /* cast to int when setting value */
 
110
#define SU_TYPE_STRING          (1 << 16)       /* cast to string */
 
111
#define SU_TYPE_TIME            (2 << 16)       /* cast to int */
 
112
#define SU_TYPE_CMD                     (3 << 16)       /* instant command */
 
113
#define SU_TYPE(t)                      ((t)->flags & 7 << 16)
 
114
 
 
115
#define SU_CMD_MASK                     0x2000
 
116
 
 
117
#define SU_VAR_COMMUNITY        "community"
 
118
#define SU_VAR_VERSION          "snmp_version"
 
119
#define SU_VAR_MIBS                     "mibs"
 
120
#define SU_VAR_SDTYPE           "sdtype"
 
121
#define SU_VAR_POLLFREQ         "pollfreq"
 
122
 
 
123
#define SU_INFOSIZE             128
 
124
#define SU_BUFSIZE              32
 
125
#define SU_LARGEBUF             256
 
126
 
 
127
#define SU_STALE_RETRY  10              /* retry to retrieve stale element */
 
128
                                                                /* after this number of iterations. */
 
129
/* modes to snmp_ups_walk. */
 
130
#define SU_WALKMODE_INIT        0
 
131
#define SU_WALKMODE_UPDATE      1
 
132
 
 
133
/* log spew limiters */
 
134
#define SU_ERR_LIMIT 10 /* start limiting after this many errors in a row  */
 
135
#define SU_ERR_RATE 100 /* only print every nth error once limiting starts */
 
136
 
 
137
typedef struct {
 
138
        snmp_info_t *snmp_info; /* pointer to the good Snmp2Nut lookup data */
 
139
        
 
140
} mib2nut_info;
 
141
 
 
142
/* Common SNMP functions */
 
143
void nut_snmp_init(const char *type, const char *host, const char *community);
 
144
void nut_snmp_cleanup(void);
 
145
struct snmp_pdu *nut_snmp_get(const char *OID);
 
146
bool nut_snmp_get_str(const char *OID, char *buf, size_t buf_len);
 
147
bool nut_snmp_get_int(const char *OID, long *pval);
 
148
bool nut_snmp_set(const char *OID, char type, const char *value);
 
149
bool nut_snmp_set_str(const char *OID, const char *value);
 
150
bool nut_snmp_set_int(const char *OID, long value);
 
151
void nut_snmp_perror(struct snmp_session *sess,  int status,
 
152
        struct snmp_pdu *response, const char *fmt, ...);
 
153
                
 
154
void su_startup(void);
 
155
void su_cleanup(void);
 
156
void su_init_instcmds(void);
 
157
void su_setuphandlers(void); /* need to deal with external function ptr */
 
158
void su_setinfo(const char *type, const char *value, int flags, int auxdata);
 
159
void su_status_set(snmp_info_t *, long value);
 
160
snmp_info_t *su_find_info(const char *type);
 
161
bool snmp_ups_walk(int mode);
 
162
bool su_ups_get(snmp_info_t *su_info_p);
 
163
 
 
164
void load_mib2nut(char *mib);
 
165
        
 
166
char *su_find_infoval(info_lkp_t *oid2info, long value);
 
167
long su_find_valinfo(info_lkp_t *oid2info, char* value);
 
168
 
 
169
int su_setvar(const char *varname, const char *val);
 
170
int su_instcmd(const char *cmdname, const char *extradata);
 
171
void su_shutdown_ups(void);
 
172
 
 
173
void read_mibconf(char *mib);
 
174
 
 
175
extern char *upsname;
 
176
struct snmp_session g_snmp_sess, *g_snmp_sess_p;
 
177
char *OID_pwr_status;
 
178
int g_pwr_battery;
 
179
int pollfreq; /* polling frequency */