aboutsummaryrefslogtreecommitdiff
path: root/src/mysqlstat_mib.mib2c
blob: 510d9bfb0ec4116df698c4b3646b70d6f8e66ebb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# This file is part of Mysqlstat -*- c -*-
# Copyright (C) 2014-2016 Sergey Poznyakoff
#
# Mysqlstat is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# Mysqlstat is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mysqlstat.  If not, see <http://www.gnu.org/licenses/>.

@define ROCOM@
/* Local variables:
   buffer-read-only: t
   End:
   vi: set ro:
*/
@enddefine@
@startperl@
$vars{'mysqlstat_translate_table'} = {
	processTotalCount       => q{process_total_count()},
	processActiveCount      => q{process_active_count()},
	processSlaveCount       => q{process_slave_count()}
};

$vars{'mysqlstat_translate'} = sub {
    my ($name) = @_;
    my $r = $vars{'mysqlstat_translate_table'}->{$name};
    if (!defined($r)) {
	print STDERR "no translation for $name!\n";
	exit(1);
    }
    $vars{'mysqlstat_get'}  = $r;
    return 0;
};	
	
$vars{'modulename'} = $vars{'name'};
$vars{'modulename'} =~ s#.*/##;
$vars{'modulename'} =~ s/\.c$//;

0;
@endperl@
@open ${modulename}.h@
/* THIS FILE IS GENERATED AUTOMATICALLY.  PLEASE DO NOT EDIT. */

extern unsigned mysqlstat_cache_timeout;

@open ${name}@
/* THIS FILE IS GENERATED AUTOMATICALLY.  PLEASE DO NOT EDIT. */

#include "mysqlstat.h"
#include "mysqlstat_mib.h"

/* Variable handlers.

   An instance handler only hands us one request at a time, unwrapping
   any eventual GETNEXT requests.  
*/

@foreach $i scalar@
  @startperl@
    &{$vars{'mysqlstat_translate'}}($vars{'i'});
  @endperl@

static int
handle_$i(netsnmp_mib_handler *handler,
                          netsnmp_handler_registration *reginfo,
                          netsnmp_agent_request_info   *reqinfo,
                          netsnmp_request_info         *requests)
{
  @if $i.settable@
    int ret;
  @end@
  @if $i.type eq 'ASN_OCTET_STR'@
    @eval $mysqlstat_type = q{char const *};@
  @elsif $i.type eq 'ASN_COUNTER64'@
    @eval $mysqlstat_type = q{uint64_t};@
  @elsif $i.type eq 'ASN_COUNTER'@
    @eval $mysqlstat_type = q{uint32_t};@
  @elsif $i.type eq 'ASN_INTEGER'@
    @eval $mysqlstat_type = q{uint32_t};@
  @elsif $i.type eq 'ASN_GAUGE'@
    @eval $mysqlstat_type = q{uint32_t};@
  @elsif $i.type eq 'ASN_TIMETICKS'@
    @eval $mysqlstat_type = q{uint32_t};@
  @end@
    $mysqlstat_type val = $mysqlstat_get;

    switch(reqinfo->mode) {
    case MODE_GET:
  @if $i.type eq 'ASN_COUNTER64'@
    {
        struct counter64 ctr;
	ctr.high = val >> 32;
	ctr.low = val & 0xffffffff;
	snmp_set_var_typed_value(requests->requestvb, $i.type,
				 &ctr,
				 sizeof(ctr));
    }				 
  @elsif $i.type eq 'ASN_OCTET_STR'@
    {
       snmp_set_var_typed_value(requests->requestvb, $i.type,
 	                         val, strlen(val));
    }				
  @elsif $i.type eq 'ASN_COUNTER'@
    {
	snmp_set_var_typed_value(requests->requestvb, $i.type,
				 &val,
				 sizeof(val));
    }
  @elsif $i.type eq 'ASN_INTEGER'@
    {
	snmp_set_var_typed_value(requests->requestvb, $i.type,
				 &val,
				 sizeof(val));
    }
  @elsif $i.type eq 'ASN_GAUGE'@
    {
	snmp_set_var_typed_value(requests->requestvb, $i.type,
				 &val,
				 sizeof(val));
    }
  @elsif $i.type eq 'ASN_TIMETICKS'@
    {
	snmp_set_var_typed_value(requests->requestvb, $i.type,
				 &val,
				 sizeof(val));
    }
  @else@
    @printf "#error \"unrecognized type %s for %s\"" $i.type $i@
  @end@
        break;

  @if $i.settable@
    /*
     * SET REQUEST
     *
     * multiple states in the transaction.  See:
     * http://www.net-snmp.org/tutorial-5/toolkit/mib_module/set-actions.jpg
     */
    case MODE_SET_RESERVE1:
	/* or you could use netsnmp_check_vb_type_and_size instead */
	ret = netsnmp_check_vb_type(requests->requestvb, $i.type);
        if (ret != SNMP_ERR_NOERROR)
	    netsnmp_set_request_error(reqinfo, requests, ret);
	break;

    case MODE_SET_RESERVE2:
    @if $mysqlstat_set_reserve2 ne ''@
        if ($mysqlstat_set_reserve2 (reqinfo, requests, vd)) {
	    netsnmp_set_request_error(reqinfo, requests,
				      SNMP_ERR_RESOURCEUNAVAILABLE);
	}
    @end@
	break;

    case MODE_SET_FREE:
    @if $mysqlstat_set_free ne ''@
        # Free resources allocated in RESERVE1 and/or
	# RESERVE2.  Something failed somewhere, and the states
	# below won't be called.
        $mysqlstat_set_free(reqinfo, requests, vd);
    @end@
	break;

    case MODE_SET_ACTION:
    @if $mysqlstat_set_action ne ''@
        ret = $mysqlstat_set_action(reqinfo, requests, vd);
        if (ret != SNMP_ERR_NOERROR)
	    netsnmp_set_request_error(reqinfo, requests, ret);
    @end@
	break;

    case MODE_SET_COMMIT:
    @if $mysqlstat_set_commit ne ''@
	# delete temporary storage
        if ($mysqlstat_set_commit(reqinfo, requests, vd))
	    netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_COMMITFAILED);
    @end@
	break;

    case MODE_SET_UNDO:
    @if $mysqlstat_set_undo ne ''@
	# UNDO and return to previous value for the object
        if ($mysqlstat_set_undo(reqinfo, requests, vd))
	    netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_UNDOFAILED);
    @end@
	break;
  @end@

    default:
	/* we should never get here, so this is a really bad error */
	snmp_log(LOG_ERR, "unknown mode (%d) in handle_${i}\n", reqinfo->mode );
	return SNMP_ERR_GENERR;
    }
    
    return SNMP_ERR_NOERROR;
}
@end@

@foreach $i table@
  ## Determine the first/last column names
  @eval $first_column = "-"@
  @eval $last_column = "-"@
  @foreach $c column@
    @if $c.readable@
      @if "$first_column" eq "-"@
        @eval $first_column = $c@
      @end@
      @eval $last_column = $c@
    @end@
  @end@

@push@
@append ${modulename}.h@
/* column number definitions for table $i */
  @foreach $c column@
#define COLUMN_$c.uc		$c.subid
  @end@

struct ${i}_entry {
    /* Index values */
    @foreach $idx index@
     @if $idx.needlength@
      @startperl@
      &{$vars{'set_field_dim'}}($vars{'idx'});
      @endperl@
      @if $field_dim > 0@
    $idx.decl $idx[$field_dim];
      @else@
    $idx.decl *$idx;
      @end@
    size_t ${idx}_len;
     @else@
    $idx.decl $idx;
     @end@
    @end@

    /* Column values */
    @foreach $c nonindex@
     @if $c.readable@
      @if $c.needlength@
      @startperl@
      &{$vars{'set_field_dim'}}($vars{'c'});
      @endperl@
      @if $field_dim > 0@
    $c.decl ${c}[$field_dim];
      @else@
    $c.decl *$c;
      @end@
    size_t ${c}_len;
      @else@
    $c.decl $c;
      @end@
     @end@
    @end@
};

int ${i}_load(netsnmp_cache *cache, void *vmagic);
void ${i}_entry_free(void *data);
@pop@

static void
${i}_free(netsnmp_cache *cache, void *vmagic)
{
	netsnmp_tdata  *table = (netsnmp_tdata *) vmagic;
	netsnmp_tdata_row *row;

	while ((row = netsnmp_tdata_row_first(table))) {
		${i}_entry_free(row->data);
		SNMP_FREE(row->data);
		netsnmp_tdata_remove_and_delete_row(table, row);
	}
}

/** handles requests for the $i table */
static int
handle_table_${i}(
    netsnmp_mib_handler               *handler,
    netsnmp_handler_registration      *reginfo,
    netsnmp_agent_request_info        *reqinfo,
    netsnmp_request_info              *requests)
{
    netsnmp_request_info       *request;
    netsnmp_table_request_info *table_info;
    struct ${i}_entry          *table_entry;

    switch (reqinfo->mode) {
    case MODE_GET:
        for (request = requests; request; request = request->next) {
            table_entry = (struct ${i}_entry *)
                           netsnmp_tdata_extract_entry(request);
            table_info  =  netsnmp_extract_table_info(request);
    
            switch (table_info->colnum) {
            @foreach $c column@
              @if $c.readable@
            case COLUMN_$c.uc:
                if (!table_entry) {
                    netsnmp_set_request_error(reqinfo, request,
                                              SNMP_NOSUCHINSTANCE);
                    continue;
                }
                @if $c.needlength@
                snmp_set_var_typed_value(request->requestvb, $c.type,
                                         table_entry->$c,
                                         table_entry->${c}_len);
                @elsif $c.type eq 'ASN_COUNTER64'@@
	        snmp_set_var_typed_value(requests->requestvb, $c.type,
				         &table_entry->$c,
				         sizeof(table_entry->$c));
                @else@
                snmp_set_var_typed_integer(request->requestvb, $c.type,
                                           table_entry->$c);
                @end@
                break;
              @end@
            @end@
            default:
                netsnmp_set_request_error(reqinfo, request,
                                          SNMP_NOSUCHOBJECT);
                break;
            }
        }
        break;
    default:
	/* we should never get here, so this is a really bad error */
	snmp_log(LOG_ERR, "unknown mode (%d) in handle_table_${i}\n",
                 reqinfo->mode);
	return SNMP_ERR_GENERR;

    }
    return SNMP_ERR_NOERROR;
}

/** Initialize the $i table by defining its contents and how it's structured */
static void
initialize_table_$i(void)
{
    const oid ${i}_oid[] = {$i.commaoid};
    const size_t ${i}_oid_len   = OID_LENGTH(${i}_oid);
    netsnmp_handler_registration    *reg;
    netsnmp_tdata                   *table_data;
    netsnmp_table_registration_info *table_info;
    netsnmp_cache                   *cache;

    DEBUGMSGTL(("${modulename}", "initializing table $i\n"));

    reg = netsnmp_create_handler_registration("$i", handle_table_${i},
	                                      ${i}_oid, ${i}_oid_len,
                                              HANDLER_CAN_RONLY);

    table_data = netsnmp_tdata_create_table("$i", 0);
    if (!table_data) {
        snmp_log(LOG_ERR,"error creating tdata table for $i\n");
        return;
    }
    cache = netsnmp_cache_create(mysqlstat_cache_timeout,
                                  ${i}_load, ${i}_free,
                                  ${i}_oid, ${i}_oid_len);
    if (!cache) {
        snmp_log(LOG_ERR,"error creating cache for $i\n");
    } else
        cache->magic = (void *)table_data;
    table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info);
    if (!table_info) {
        snmp_log(LOG_ERR,"error creating table info for $i\n");
        return;
    }
    netsnmp_table_helper_add_indexes(table_info,
    @foreach $idx index@
                           $idx.type,  /* index: $idx */
    @end@
                           0);

    table_info->min_column = COLUMN_$first_column.uc;
    table_info->max_column = COLUMN_$last_column.uc;
    
    netsnmp_tdata_register(reg, table_data, table_info);
    if (cache) 
	netsnmp_inject_handler(reg, netsnmp_cache_handler_get(cache));
/*	
        netsnmp_inject_handler_before(reg, netsnmp_cache_handler_get(cache),
                                      TABLE_TDATA_NAME);
*/
}

@end@

/** Initializes the $name module */
void
init_$modulename(void)
{
  @foreach $i scalar@
    @startperl@
      &{$vars{'mysqlstat_translate'}}($vars{'i'});
    @endperl@
$mysqlstat_if
    const oid ${i}_oid[] = { $i.commaoid };
$mysqlstat_endif
  @end@

  DEBUGMSGTL(("$modulename", "Initializing\n"));
  
  @foreach $i scalar@
    @startperl@
      &{$vars{'mysqlstat_translate'}}($vars{'i'});
    @endperl@
    netsnmp_register_scalar(
        netsnmp_create_handler_registration("$i", handle_$i,
                               ${i}_oid, OID_LENGTH(${i}_oid),
    @if !$i.settable@
                               HANDLER_CAN_RONLY
    @end@
    @if $i.settable@
                               HANDLER_CAN_RWRITE
    @end@
        ));
  @end@

  @foreach $i table@
    initialize_table_$i();
  @end@
}

void
deinit_$modulename(void)
{
	/* Nothing */
}
@calldefine ROCOM@


Return to:

Send suggestions and report system problems to the System administrator.