|
| 1 | +# vim: ts=4: sts=4: sw=4: expandtab |
| 2 | +# Copyright 2024 dhtech |
| 3 | +# |
| 4 | +# Use of this source code is governed by a BSD-style |
| 5 | +# license that can be found in the LICENSE file |
| 6 | +import lib |
| 7 | +import os |
| 8 | +import sqlite3 |
| 9 | +import yaml |
| 10 | + |
| 11 | +DB_FILE = '/etc/ipplan.db' |
| 12 | + |
| 13 | +def get_sflow_clients(): |
| 14 | + if os.path.isfile(DB_FILE): |
| 15 | + try: |
| 16 | + conn = sqlite3.connect(DB_FILE) |
| 17 | + db = conn.cursor() |
| 18 | + except sqlite3.Error as e: |
| 19 | + print("An error occurred: {}".format(e.args[0])) |
| 20 | + exit(2) |
| 21 | + else: |
| 22 | + print("No database file found: {}".format(DB_FILE)) |
| 23 | + exit(3) |
| 24 | + db.execute( |
| 25 | + "SELECT h.name AS hostname, h.ipv4_addr_txt AS ipv4_addr ,h.ipv6_addr_txt AS ipv6_addr, o2.value AS layer " |
| 26 | + "FROM host h " |
| 27 | + "INNER JOIN option o1 ON h.node_id = o1.node_id " |
| 28 | + "INNER JOIN option o2 ON h.node_id = o2.node_id " |
| 29 | + "WHERE o1.name='pkg' AND o1.value='sflowclient' " |
| 30 | + "AND o2.name='layer';" |
| 31 | + ) |
| 32 | + res = db.fetchall() |
| 33 | + if not res: |
| 34 | + return None |
| 35 | + |
| 36 | + column_names = [description[0] for description in db.description] |
| 37 | + conn.close() |
| 38 | + rows_dict = [dict(zip(column_names, row)) for row in res] |
| 39 | + |
| 40 | + return rows_dict |
| 41 | + |
| 42 | +def get_snmpv2_providers(): |
| 43 | + providers = [] |
| 44 | + clients = get_sflow_clients() |
| 45 | + if not clients: |
| 46 | + return providers |
| 47 | + current_event = lib.get_current_event() |
| 48 | + for client in clients: |
| 49 | + key = current_event+'-mgmt/snmpv2:'+client['layer'] |
| 50 | + secrets = lib.read_secret(key) |
| 51 | + if not secrets: |
| 52 | + return providers |
| 53 | + if "community" in secrets: |
| 54 | + provider = { |
| 55 | + "ipv4": client["ipv4_addr"], |
| 56 | + "community": secrets["community"], |
| 57 | + } |
| 58 | + providers.append(provider) |
| 59 | + return providers |
| 60 | + |
| 61 | +def get_snmpv3_providers(): |
| 62 | + providers = [] |
| 63 | + clients = get_sflow_clients() |
| 64 | + if not clients: |
| 65 | + return providers |
| 66 | + current_event = lib.get_current_event() |
| 67 | + for client in clients: |
| 68 | + key = current_event+'-mgmt/snmpv3:'+client['layer'] |
| 69 | + secrets = lib.read_secret(key) |
| 70 | + if not secrets: |
| 71 | + return providers |
| 72 | + if "user" in secrets: |
| 73 | + provider = { |
| 74 | + "ipv4": client["ipv4_addr"], |
| 75 | + "authentication-passphrase": secrets["auth"], |
| 76 | + "authentication-protocol": secrets["authtype"].replace(" ","").upper(), |
| 77 | + "privacy-passphrase": secrets["priv"], |
| 78 | + "privacy-protocol": secrets["privtype"].replace(" ","").replace("128","").upper(), |
| 79 | + "user": secrets["user"], |
| 80 | + } |
| 81 | + providers.append(provider) |
| 82 | + return providers |
| 83 | + |
| 84 | +def get_prefixes(ipversion): |
| 85 | + if os.path.isfile(DB_FILE): |
| 86 | + try: |
| 87 | + conn = sqlite3.connect(DB_FILE) |
| 88 | + db = conn.cursor() |
| 89 | + except sqlite3.Error as e: |
| 90 | + print("An error occurred: {}".format(e.args[0])) |
| 91 | + exit(2) |
| 92 | + else: |
| 93 | + print("No database file found: {}".format(DB_FILE)) |
| 94 | + exit(3) |
| 95 | + |
| 96 | + if ipversion == "4": |
| 97 | + db.execute( |
| 98 | + 'SELECT SUBSTR(name,1, INSTR(name, "@")-1) AS location, name, short_name, ipv4_txt' |
| 99 | + ' FROM network' |
| 100 | + ' WHERE node_id NOT IN (SELECT option.node_id FROM option WHERE name = "no-akv")' |
| 101 | + ' AND name LIKE "%@%" AND ipv4_txt IS NOT NULL' |
| 102 | + ) |
| 103 | + |
| 104 | + elif ipversion == "6": |
| 105 | + db.execute( |
| 106 | + 'SELECT SUBSTR(name,1, INSTR(name, "@")-1) AS location, name, short_name, ipv6_txt' |
| 107 | + ' FROM network' |
| 108 | + ' WHERE node_id NOT IN (SELECT option.node_id FROM option WHERE name = "no-akv")' |
| 109 | + ' AND name LIKE "%@%" AND ipv6_txt IS NOT NULL' |
| 110 | + ' AND NOT (name = "BOGAL@DREAMHACK" AND ipv6_txt = "2a05:2240:5000::/48")' |
| 111 | + ) |
| 112 | + else: |
| 113 | + raise NetworkTypeNotFoundError('network type must be 4 or 6') |
| 114 | + |
| 115 | + res = db.fetchall() |
| 116 | + if not res: |
| 117 | + raise NetworkNotFoundError('network not found') |
| 118 | + |
| 119 | + column_names = [description[0] for description in db.description] |
| 120 | + conn.close() |
| 121 | + rows_dict = [dict(zip(column_names, row)) for row in res] |
| 122 | + |
| 123 | + return rows_dict |
| 124 | + |
| 125 | + |
| 126 | +def requires(host, *args): |
| 127 | + return ['apache(ldap)'] |
| 128 | + |
| 129 | + |
| 130 | +def generate(host, *args): |
| 131 | + |
| 132 | + info = {} |
| 133 | + info['snmpv3_providers'] = get_snmpv3_providers() |
| 134 | + info['snmpv2_providers'] = get_snmpv2_providers() |
| 135 | + info['current_event'] = lib.get_current_event() |
| 136 | + info['ipv6_prefixes'] = get_prefixes('6') |
| 137 | + info['ipv4_prefixes'] = get_prefixes('4') |
| 138 | + return {'akvorado': info} |
0 commit comments