-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipam2lnms.py
executable file
·183 lines (153 loc) · 6.85 KB
/
ipam2lnms.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright 2019 Dag Bakke <[email protected]>
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
# This script compares the hostnames found in the devices tables
# of phpIPAM and LibreNMS. phpIPAM is considered the master.
#
# This script will not write to the phpIPAM db, and may change the
# LibreNMS only if the '-c' switch is set.
#
#
# See README.md for further information.
#
# dag - at - bakke - dot - com
import sys
import argparse
import pymysql as mdb
import subprocess
############ stuff which may need tuning by you ################
dbcreds = '~/.my.cnf' # See README.md
# these denotes stanzas in $dbcreds
librenms = 'librenms'
phpipam = 'phpipam'
# tune these two to return only the hosts that matter to you, in column 0.
sql_query_librenms = '''
SELECT hostname
FROM devices
WHERE hostname LIKE "%-fw"
'''
sql_query_phpipam = '''
SELECT hostname
FROM devices
WHERE hostname LIKE "%-fw"
'''
add_options = 'ap v3' # default SNMP options
############# end ###################
addcmd = '/opt/librenms/addhost.php'
delcmd = '/opt/librenms/delhost.php'
ignorecmd = '/opt/librenms/ignorehost.php' # I would like this command to exist
disablecmd = '/opt/librenms/disablehost.php' # This too.
def getdbdata(query, db, verbose):
mylist = []
try:
con = mdb.connect(read_default_file=dbcreds, read_default_group=db)
cursor = con.cursor()
cursor.execute(query)
rows = cursor.fetchall()
for row in rows:
mylist.append(row[0])
if verbose:
if len(mylist) == 0:
print(f"{db}\t: query returns no data")
if len(mylist) > 0:
print(f"{db}\t: ok")
except Exception as ex:
print(f"{db}\t: got an exception" + str(ex))
finally:
if con:
con.close()
return mylist
def main(args):
parser = argparse.ArgumentParser()
parser.add_argument("-v", "--verbose", action='store_true', default=False,
help="show verbose output")
parser.add_argument("-a", "--addhosts", action='store_true', default=False,
help="hosts only found in phpipam will be added to lnms")
parser.add_argument("-d", "--deletehosts", action='store_true', default=False,
help="hosts not found in phpipam will be deleted from lnms")
parser.add_argument("-i", "--ignorehosts", action='store_true', default=False,
help="hosts not found in phpipam will be marked as ignored in lnms. (dummy for now)")
parser.add_argument("-z", "--disablehosts", action='store_true', default=False,
help="hosts not found in phpipam will be marked as disabled in lnms. (dummy for now)")
parser.add_argument("-c", "--commit", action='store_true', default=False,
help="actually execute commands")
parser.add_argument("-f", "--force", action='store_true', default=False,
help="forces the device to be added by skipping the icmp and snmp check against the host.")
parser.add_argument("-b", action='store_true', default=False,
help="Add the host with SNMP if it replies to it, otherwise only ICMP.")
parser.add_argument("-P", action='store_true', default=False,
help="Add the host with only ICMP, no SNMP or OS discovery.")
myargs = parser.parse_args()
hosts_unique_to_librenms = []
hosts_unique_to_phpipam = []
hosts_in_librenms = getdbdata(sql_query_librenms, librenms, myargs.verbose)
hosts_in_phpipam = getdbdata(sql_query_phpipam, phpipam, myargs.verbose)
for host in hosts_in_librenms:
if host not in hosts_in_phpipam:
hosts_unique_to_librenms.append(host)
for host in hosts_in_phpipam:
if host not in hosts_in_librenms:
hosts_unique_to_phpipam.append(host)
if myargs.verbose:
print("\nhosts unique to librenms:\n" + str(hosts_unique_to_librenms))
print("\nhosts unique to phpipam:\n" + str(hosts_unique_to_phpipam) + "\n")
forceopts = ""
if myargs.force:
forceopts += "f"
if myargs.b:
forceopts += "b"
if myargs.P:
forceopts += "P"
if forceopts:
forceopts = "-" + forceopts
if not myargs.commit: # pretend
print("This is what will happen if you add the '-c' switch:")
for host in hosts_unique_to_phpipam:
if myargs.addhosts:
print(f"{addcmd} {forceopts} {host} {add_options}")
for host in hosts_unique_to_librenms:
if myargs.ignorehosts:
print(f"{ignorecmd} {host} <- dummy")
if myargs.disablehosts:
print(f"{disablecmd} {host} <- dummy")
if myargs.deletehosts:
print(f"{delcmd} {host}")
else: # myargs.commit is true
for host in hosts_unique_to_phpipam:
if myargs.addhosts:
add_result = subprocess.run([addcmd, forceopts, host, add_options], check=False, stdout=subprocess.PIPE, shell=False, stderr=subprocess.STDOUT)
if myargs.verbose:
print(str(add_result.stdout.decode('UTF-8')).rstrip())
for host in hosts_unique_to_librenms: # ignore, disable or delete hosts
# if myargs.ignorehosts:
# ignore_result = subprocess.run([ignorecmd, host], check=False, stdout=subprocess.PIPE, shell=False, stderr=subprocess.STDOUT)
# if myargs.verbose:
# print(str(ignore_result.stdout.decode('UTF-8')).rstrip())
# if myargs.disablehosts:
# disable_result = subprocess.run([disablecmd, host], check=False, stdout=subprocess.PIPE, shell=False, stderr=subprocess.STDOUT)
# if myargs.verbose:
# print(str(disable_result.stdout.decode('UTF-8')).rstrip())
if myargs.deletehosts:
delete_result = subprocess.run([delcmd, host], check=False, stdout=subprocess.PIPE, shell=False, stderr=subprocess.STDOUT)
if myargs.verbose:
print(str(delete_result.stdout.decode('UTF-8')).rstrip())
if __name__ == '__main__':
sys.exit(main(sys.argv))