-
Notifications
You must be signed in to change notification settings - Fork 1
/
snmp-3com-agent-daemon
executable file
·115 lines (90 loc) · 3.64 KB
/
snmp-3com-agent-daemon
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
#!/usr/bin/python3 -tt
# Twisted hosts our website and helps with async tasks.
# The application threads are structured in the following way:
#
# reactor
# `-- agent (1 thread)
# `-- workers (2 threads)
#
from twisted.internet import reactor
# from twisted.web.wsgi import WSGIResource
# from twisted.web.server import Site
# from twisted.python.threadpool import ThreadPool
from twisted.python import log
# Data are accessed through SQLSoup, using SQLAlchemy.
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy import create_engine
# Command line arguments follow the GNU conventions.
from getopt import gnu_getopt
from sys import argv, stderr
# Configuration is stored in a simple ini file.
from configparser import ConfigParser
# Import all the application handles.
from adminator import SNMP3comAgent
from sqlmodel import Session
if __name__ == '__main__':
def do_start(config):
# Start Twisted logging to console.
log.startLogging(stderr)
# Read database configuration options.
db_url = config.get('database', 'url')
# Default to much saner database query defaults and always
# commit and/or flush statements explicitly.
# unused
# factory = sessionmaker(autocommit=False, autoflush=False)
# Prepare database connection with table reflection.
engine = create_engine(db_url, echo=True)
# factory to create scoped_session of type Session (from sqlmodel, not sqlalchemy)
# to avoid multi-threading problems in new versions of things
def sqlmodel_factory():
return Session(engine, autoflush=False)
db = scoped_session(sqlmodel_factory)
#db.schema = 'topology'
# Extract agent options, sans the pool_size we handle here.
agent_opts = dict(config.items('snmp-3com'))
# agent_opts = dict()
agent_pool = int(agent_opts.pop('pool_size', 2))
# Set the correct thread pool size for the agent.
reactor.suggestThreadPoolSize(agent_pool)
# Prepare the agent that runs in an exclusive thread.
agent = SNMP3comAgent(db, **agent_opts)
# Run the startup code as the first thing.
reactor.callLater(0, agent.start)
# Run the Twisted reactor until the user terminates us.
reactor.run()
def do_help(*args, **kwargs):
print('Usage: ifstatus-agent-daemon [--config=/etc/adminator.ini]')
print('')
print('The agent synchronizes interface status to database.')
print('')
print('OPTIONS:')
print(' --help, -h Display this help.')
print(' --version, -V Display version info.')
print('')
print(' --config, -c file Load alternative configuration file.')
print('')
print('Report bugs at <http://github.com/techlib/adminator>.')
def do_version(*args, **kwargs):
print('snmp3com-agent-daemon (NTK) 0.1')
# Parse command line arguments.
opts, args = gnu_getopt(argv, 'hVc:', ['help', 'version', 'config='])
action = do_start
config_path = '/etc/adminator.ini'
for k, v in opts:
if k in ('--help', '-h'):
action = do_help
elif k in ('--version', '-V'):
action = do_version
elif k in ('--config', '-c'):
config_path = v
# Load the configuration from file.
if action not in (do_help, do_version):
config = ConfigParser()
config.read(config_path)
# Load the configuration from file.
config = ConfigParser()
config.read(config_path)
# Perform the selected action.
action(config=config)
# vim:set sw=4 ts=4 et:
# -*- coding: utf-8 -*-