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
|
#!/usr/bin/env python3
#
# Copyright (C) 2018 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
# File: snmp.py
# Purpose:
# Show SNMP community/remote hosts
# Used by the "run show snmp community" commands.
import os
import sys
import argparse
from vyos.config import Config
from vyos.util import call
config_file_daemon = r'/etc/snmp/snmpd.conf'
parser = argparse.ArgumentParser(description='Retrieve infomration from running SNMP daemon')
parser.add_argument('--allowed', action="store_true", help='Show available SNMP communities')
parser.add_argument('--community', action="store", help='Show status of given SNMP community', type=str)
parser.add_argument('--host', action="store", help='SNMP host to connect to', type=str, default='localhost')
config = {
'communities': [],
}
def read_config():
with open(config_file_daemon, 'r') as f:
for line in f:
# Only get configured SNMP communitie
if line.startswith('rocommunity') or line.startswith('rwcommunity'):
string = line.split(' ')
# append community to the output list only once
c = string[1]
if c not in config['communities']:
config['communities'].append(c)
def show_all():
if len(config['communities']) > 0:
print(' '.join(config['communities']))
def show_community(c, h):
print('Status of SNMP community {0} on {1}'.format(c, h), flush=True)
call('/usr/bin/snmpstatus -t1 -v1 -c {0} {1}'.format(c, h))
if __name__ == '__main__':
args = parser.parse_args()
# Do nothing if service is not configured
c = Config()
if not c.exists_effective('service snmp'):
print("SNMP service is not configured")
sys.exit(0)
read_config()
if args.allowed:
show_all()
sys.exit(1)
elif args.community:
show_community(args.community, args.host)
sys.exit(1)
else:
parser.print_help()
sys.exit(1)
|