Project

General

Profile

Convert a MIB file to a Zabbix template » History » Version 1

offbyone, 2022-08-11 16:00
Initial version

1 1 offbyone
h1. Convert a MIB file to a Zabbix template
2
3
{{>toc}}
4
5
6
h2. Synopsis
7
8
Did you ever have to monitor a new device by SNMP? Do you have a MIB file that describes what that device can report, but you don't feel like translating that MIB by hand to Zabbix monitoring? Fear not, some hero has written a script that takes a MIB file and spits out a Zabbix template, ready for import!
9
10
Read on to find out how to do this.
11
12
13
h2. Procedure
14
15
This guide is based on https://sbcode.net/zabbix/mib-to-zabbix-template/.
16
17
18
h3. Install and configure SNMP tools
19
20
First, the SNMP tools and Perl SNMP library must be installed.
21
22
|_.Debian/Ubuntu|_.CentOS/Red Hat/Rocky Linux|
23
|<pre>
24
</notextile>*sudo apt-get update*<notextile>
25
</notextile>*sudo apt install snmp*<notextile>
26
</notextile>*sudo apt install snmp-mibs-downloader*<notextile>
27
</pre>|<pre>
28
</notextile>*yum check-update*<notextile>
29
</notextile>*yum install net-snmp-utils*<notextile>
30
</notextile>*yum install net-snmp-libs*<notextile>
31
</pre>|
32
33
To enable parsing of MIB files (it's disabled by default), edit @/etc/snmp/snmp.conf@ and comment out the line "@mibs@" or "@mibs :@".
34
35
Test if it works:
36
|_.Success|_.Failure|
37
|<pre>
38
$ </notextile>*snmptranslate 1.3.6.1.2.1.1*<notextile>
39
SNMPv2-MIB::system
40
</pre>|<pre>
41
$ </notextile>*snmptranslate 1.3.6.1.2.1.1*<notextile>
42
iso.3.6.1.2.1.1
43
</pre>|
44
45
46
h3. Install Perl and the mib2zabbix conversion script
47
48
The @mib2zabbix.pl@ script is written in Perl, so we need to install it and some Perl modules that are used by the script.
49
50
|_.Debian/Ubuntu|_.CentOS/Red Hat/Rocky Linux|
51
|<pre>
52
</notextile>*apt-get install perl libxml-simple-perl libsnmp-perl*<notextile>
53
</pre>|<pre>
54
</notextile>*yum install "perl(SNMP)" "perl(XML::Simple)"*<notextile>
55
</pre>|
56
57
The @mib2zabbix.pl@ script was originally written by someone else (https://github.com/zabbix-tools/mib2zabbix), but the last activity on that repo was in December 2017.
58
I have forked the project and made some improvements to the code, so it works without warnings on a modern version of Perl.
59
60
Fetch the script:
61
|_.Using @git@|_.Just the script|
62
|<pre>
63
</notextile>*git clone https://github.com/sputtene/mib2zabbix.git*<notextile>
64
</notextile>*cd mib2zabbix*<notextile>
65
</pre>|<pre>
66
</notextile>*curl https://raw.githubusercontent.com/sputtene/mib2zabbix/master/mib2zabbix.pl*<notextile>
67
</pre>|
68
69
Make the script executable:
70
<pre>
71
</notextile>*chmod +x mib2zabbix.pl*<notextile>
72
</pre>
73
74
Test it by showing the usage information:
75
<pre>
76
$ </notextile>*./mib2zabbix.pl --help*<notextile>
77
Usage:
78
    mib2zabbix.pl -o <OID> [OPTIONS]...
79
80
    Export loaded SNMP MIB OIDs to Zabbix Template XML
81
82
        -f, --filename=PATH         output filename (default: stdout)
83
84
        -N, --name=STRING           template name (default: OID label)
85
        -G, --group=STRING          template group (default: 'Templates')
86
        -e, --enable-items          enable all template items (default: disabled)
87
88
        -o, --oid=STRING            OID tree root to export
89
90
        -v, --snmpver=1|2|3         SNMP version (default: 2)
91
        -p, --port=PORT             SNMP UDP port number (default: 161)
92
93
    SNMP Version 1 or 2c specific
94
95
        -c, --community=STRING      SNMP community string (default: 'public')
96
97
    SNMP Version 3 specific
98
99
        -L, --level=LEVEL           security level (noAuthNoPriv|authNoPriv|authPriv)
100
        -n, --context=CONTEXT       context name
101
        -u, --username=USERNAME     security name
102
        -a, --auth=PROTOCOL         authentication protocol (MD5|SHA)
103
        -A, --authpass=PASSPHRASE   authentication protocol passphrase
104
        -x, --privacy=PROTOCOL      privacy protocol (DES|AES)
105
        -X, --privpass=PASSPHRASE   privacy passphrase
106
107
    Zabbix item configuration
108
109
        --check-delay=SECONDS       check interval in seconds (default: 60)
110
        --disc-delay=SECONDS        discovery interval in seconds (default: 3600)
111
        --history=DAYS              history retention in days (default: 7)
112
        --trends=DAYS               trends retention in days (default: 365)
113
114
        -h, --help                  print this message
115
</pre>
116
117
If you see the usage information, everything is ok. If you get an error, make sure you installed all dependencies (SNMP tools and the Perl modules) in the previous steps.
118
119
120
h3. (Original instructions) Use a single command to generate the Zabbix template XML file
121
122
From here on, the instructions on the original webpage and these instructions start to differ. The original instructions pipe the list of OIDs directly into the @mib2zabbix@ script.
123
124
These instructions use multiple steps with explicit intermediate files; I found that much easier to debug the whole procedure.
125
Also, I had to do some extra stuff (like setting environment variables) to make it work; I have not tested the one-line command below, so I don't know if it works.
126
127
If you have a single MIB file that you want to convert to a Zabbix template, you might be successful using the one-liner on the original page:
128
<pre>
129
</notextile>*snmptranslate -Tz -m ./HUAWEI-MIB.mib | ./mib2zabbix.pl -o .1.3.6.1.4.1 -f template-huawei-mib.xml -N huawei-mib*<notextile>
130
</pre>
131
132
Change the name of your MIB file, the top-level OID you want to export from, the name of the output XML file and the template name in the above command according to your situation.
133
134
135
h3. Step by step Zabbix template XML file generation
136
137
I have followed this step by step procedure.
138
139
140
h4. Set environment variables with MIB file paths
141
142
To start, we must set some environment variables that will be used by the SNMP tools and the @mib2zabbix@ script to determine which MIB files to load.
143
144
For the first step (generating a list of all OIDs) this is not strictly necessary, because we can specify the settings trough command line parameters (@-m@ and @-M@ parameters for the @snmptranslate@ command). The @mib2zabbix.pl@ doesn't provide command line switches for these settings though, so we must pass the configuration trough the environment or it will complain about missing MIB files.
145
146
In this example, I assume the MIB file(s) you want to convert to a Zabbix template are in the directory @mib_files/@.
147
148
Execute this in the shell that you will use to run all following commands:
149
<pre>
150
</notextile>*export MIBS=ALL*<notextile>
151
</notextile>*export MIBDIRS="+mib_files/"*<notextile>
152
</pre>
153
154
Pay attention to the + sign as the first character of the @MIBDIRS@ value, it is important.
155
156
157
h4. Generate a list of OIDs
158
159
First, we will generate a list of all OIDs known to the system (this includes the MIB-file(s) that we actually want to use for the Zabbix template, because of the environment variables that were just set).
160
161
Execute:
162
<pre>
163
</notextile>*snmptranslate -Tz > all_oids.list*<notextile>
164
</pre>
165
166
to generate a file @all_oids.list@ in the correct format for @mib2zabbix.pl@.
167
168
169
h4. Generate the Zabbix template
170
171
Finally, we can generate the Zabbix template. You need the top-level OID (SNMP agent and SNMP trapper items will be created for all entries in the OID subtree under this top-level OID); I'll use *.1.3.6.1.4.1.6431* here as an example.
172
173
Execute:
174
<pre>
175
</notextile>*./mib2zabbix.pl --name 'Example Template from MIB' --group 'Templates/Examples' --enable-items --check-delay=5m --disc-delay=1h --oid .1.3.6.1.4.1.6431.1 -f Example_template_from_MIB.xml < all_oids.list*<notextile>
176
</pre>
177
178
Adjust parameter values as required. Pay special attention to the @--enable-items@ parameter: without it, all items and discovery rules will be created disabled and that may not be what you want.
179
180
The file @Example_template_from_MIB.xml@ can now be imported in Zabbix through *Configuration &rarr; Templates* and then the *Import* button in the top right.