]> git.pld-linux.org Git - packages/net-snmp.git/blob - net-snmp-loadave-writable.patch
44dd7ec024a79a7f92e6dbce2d2ca3f9dc581748
[packages/net-snmp.git] / net-snmp-loadave-writable.patch
1 This patch makes "load X Y Z" snmpsettable via snmpset,
2
3 it behaves the same as sysName.0, once written in snmpd.conf, it becames
4 readonly.
5
6 Signed-off-by: Elan Ruusamäe <glen@delfi.ee>
7 Upstream-Tracker: https://sourceforge.net/tracker/?func=detail&aid=2931446&group_id=12694&atid=312694
8
9 --- net-snmp-5.4.2.1/agent/mibgroup/ucd-snmp/loadave.c~ 2009-12-21 21:00:10.094254079 +0200
10 +++ net-snmp-5.4.2.1/agent/mibgroup/ucd-snmp/loadave.c  2009-12-21 22:02:45.398903148 +0200
11 @@ -135,7 +135,19 @@
12  #include "util_funcs.h"
13  #include "kernel.h"
14  
15 -double          maxload[3];
16 +static double maxload[3];
17 +static int laConfigSet = 0;
18 +
19 +static int
20 +loadave_store_config(int a, int b, void *c, void *d)
21 +{
22 +    char line[SNMP_MAXBUF_SMALL];
23 +    if (laConfigSet > 0) {
24 +        snprintf(line, SNMP_MAXBUF_SMALL, "pload %.02f %.02f %.02f", maxload[0], maxload[1], maxload[2]);
25 +        snmpd_store_config(line);
26 +    }
27 +    return SNMPERR_SUCCESS;
28 +}
29  
30  void
31  init_loadave(void)
32 @@ -152,7 +164,7 @@
33           {ERRORNAME}},
34          {LOADAVE, ASN_OCTET_STR, RONLY, var_extensible_loadave, 1,
35           {LOADAVE}},
36 -        {LOADMAXVAL, ASN_OCTET_STR, RONLY, var_extensible_loadave, 1,
37 +        {LOADMAXVAL, ASN_OCTET_STR, RWRITE, var_extensible_loadave, 1,
38           {LOADMAXVAL}},
39          {LOADAVEINT, ASN_INTEGER, RONLY, var_extensible_loadave, 1,
40           {LOADAVEINT}},
41 @@ -179,9 +191,22 @@
42      REGISTER_MIB("ucd-snmp/loadave", extensible_loadave_variables,
43                   variable2, loadave_variables_oid);
44  
45 +    laConfigSet = 0;
46 +
47      snmpd_register_config_handler("load", loadave_parse_config,
48                                    loadave_free_config,
49                                    "max1 [max5] [max15]");
50 +
51 +    snmpd_register_config_handler("pload",
52 +                                  loadave_parse_config, NULL, NULL);
53 +
54 +
55 +    /*
56 +     * we need to be called back later
57 +     */
58 +    snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
59 +                                       loadave_store_config, NULL);
60 +
61  }
62  
63  void
64 @@ -189,6 +214,25 @@
65  {
66      int             i;
67  
68 +    if (strcmp(token, "pload") == 0) {
69 +        if (laConfigSet < 0) {
70 +            snmp_log(LOG_WARNING,
71 +                     "ignoring attempted override of read-only load\n");
72 +            return;
73 +        } else {
74 +            laConfigSet++;
75 +        }
76 +    } else {
77 +        if (laConfigSet > 0) {
78 +            snmp_log(LOG_WARNING,
79 +                     "ignoring attempted override of read-only load\n");
80 +            /*
81 +             * Fall through and copy in this value.
82 +             */
83 +        }
84 +        laConfigSet = -1;
85 +    }
86 +
87      for (i = 0; i <= 2; i++) {
88          if (cptr != NULL)
89              maxload[i] = atof(cptr);
90 @@ -309,6 +353,71 @@
91      return 0;
92  }
93  
94 +static int
95 +write_laConfig(int action,
96 +                          u_char * var_val,
97 +                          u_char var_val_type,
98 +                          size_t var_val_len,
99 +                          u_char * statP, oid * name, size_t name_len)
100 +{
101 +    static double laConfig = 0;
102 +
103 +    switch (action) {
104 +    case RESERVE1: /* Check values for acceptability */
105 +        if (var_val_type != ASN_OCTET_STR) {
106 +            DEBUGMSGTL(("ucd-snmp/loadave",
107 +                        "write to laConfig not ASN_OCTET_STR\n"));
108 +            return SNMP_ERR_WRONGTYPE;
109 +        }
110 +        if (var_val_len > 8 || var_val_len <= 0) {
111 +            DEBUGMSGTL(("ucd-snmp/loadave",
112 +                        "write to laConfig: bad length\n"));
113 +            return SNMP_ERR_WRONGLENGTH;
114 +        }
115 +
116 +        if (laConfigSet < 0) {
117 +            /*
118 +             * The object is set in a read-only configuration file.
119 +             */
120 +            return SNMP_ERR_NOTWRITABLE;
121 +        }
122 +        break;
123 +
124 +    case RESERVE2: /* Allocate memory and similar resources */
125 +        {
126 +            u_char buf[8];
127 +            int old_errno = errno;
128 +            double val;
129 +            char *endp;
130 +
131 +            strncpy(buf, var_val, var_val_len);
132 +            buf[var_val_len] = '\0';
133 +            val = strtod(buf, &endp);
134 +
135 +            if (errno == ERANGE || *endp != '\0' || val < 0 || val > 65536.00) {
136 +                errno = old_errno;
137 +                DEBUGMSGTL(("ucd-snmp/loadave",
138 +                            "write to laConfig: invalid value\n"));
139 +                return SNMP_ERR_WRONGVALUE;
140 +            }
141 +
142 +            errno = old_errno;
143 +
144 +            laConfig = val;
145 +        }
146 +        break;
147 +
148 +    case COMMIT:
149 +        {
150 +            int idx = name[name_len - 1] - 1;
151 +            maxload[idx] = laConfig;
152 +            laConfigSet = 1;
153 +        }
154 +    }
155 +
156 +    return SNMP_ERR_NOERROR;
157 +}
158 +
159  u_char         *
160  var_extensible_loadave(struct variable * vp,
161                         oid * name,
162 @@ -328,6 +437,10 @@
163      case MIBINDEX:
164          long_ret = name[*length - 1];
165          return ((u_char *) (&long_ret));
166 +    case LOADMAXVAL:
167 +        /* setup write method, but don't return yet */
168 +        *write_method = write_laConfig;
169 +        break;
170      case ERRORNAME:
171          sprintf(errmsg, "Load-%d", ((name[*length - 1] == 1) ? 1 :
172                                      ((name[*length - 1] == 2) ? 5 : 15)));
This page took 0.031658 seconds and 2 git commands to generate.