-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlite_connector.py
161 lines (146 loc) · 5.16 KB
/
sqlite_connector.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
# -*- coding: utf-8 -*-
# DNS PTR updater
# Copyright (C) 2017 Pavle Obradovic (pajaja)
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
import logging
import time
import os
import calendar
import sqlite3
from classes.connectors import BaseConnector
from classes import Ptr
__version__ = '0.4.4'
# noinspection SqlResolve
class SqliteConnector(BaseConnector):
def __init__(self, dispatcher):
BaseConnector.__init__(self, dispatcher)
self.logger = logging.getLogger('dns_update.connector.sqlite')
db_file = os.path.dirname(os.path.abspath(__file__)) + '/../../../' + self.config['db']
self.logger.debug("Database file: '%s'" % db_file)
self.connection = sqlite3.connect(db_file)
self.logger.info("Database file '%s' loaded" % db_file)
self.c = self.connection.cursor()
try:
self.c.execute("SELECT COUNT(*) FROM `ptrs`")
self.logger.info("%s existing PTRs in database." % self.c.fetchall()[0][0])
except sqlite3.OperationalError:
self.logger.warning("Table `ptrs` doesn't exists. Creating...")
self.create_ptr_table()
def create_ptr_table(self):
"""
Create PTR table
:return:
"""
sql = """CREATE TABLE IF NOT EXISTS `ptrs` (
`ip_address` INTEGER NOT NULL PRIMARY KEY ,
`hostname` VARCHAR(128) DEFAULT NULL,
`if_name` VARCHAR(128) DEFAULT NULL,
`ptr` VARCHAR(128) DEFAULT NULL,
`ptr_zone` VARCHAR(128) NOT NULL,
`status` INTEGER UNSIGNED NOT NULL,
`insert_time` INTEGER UNSIGNED DEFAULT NULL
)"""
self.c.execute(sql)
def drop_ptr_table(self):
"""
Drop PTR table
:return:
"""
sql = "DROP TABLE IF EXISTS `ptrs`"
self.c.execute(sql)
def ptr_count(self):
"""
Returns number of PTRs in database
:return:
"""
sql = "SELECT COUNT(`ip_address`) FROM `ptrs`"
self.c.execute(sql)
return self.c.fetchone()[0]
def load_ptrs(self):
"""
Load all PTRs from Database
:return:
"""
ptrs = {}
sql = "SELECT `ip_address`, `hostname`, `if_name`, `ptr`, `status` FROM `ptrs`"
self.c.execute(sql)
for ptr_row in self.c.fetchall():
ptr = Ptr(
ip_address=ptr_row[0],
hostname=ptr_row[1],
if_name=ptr_row[2],
ptr=ptr_row[3],
status=ptr_row[4]
)
ptrs[str(ptr.ip_address)] = ptr
return ptrs
def save_ptr(self, ptr, commit=True):
"""
Save a single Ptr to database
:param ptr:
:return:
"""
sql = "INSERT OR REPLACE INTO `ptrs` VALUES (" \
":ip_address, :hostname, :if_name, :ptr, :ptr_zone, :status, :insert_time" \
")"
data = {
"ip_address": ptr.ip_int(),
"hostname": ptr.hostname,
"if_name": ptr.if_name,
"ptr": ptr.ptr,
"ptr_zone": ptr.get_ptr_zone(),
"status": ptr.status,
"insert_time": calendar.timegm(ptr.time)
}
self.c.execute(sql, data)
if commit:
self.connection.commit()
def save_ptrs(self, ptrs):
"""
Save multiple Ptrs to database. Uses `save_ptr` method
:param ptrs:
:return:
"""
self.logger.info("Saving %d PTRs to database..." % len(ptrs))
for ptr in ptrs:
self.save_ptr(ptrs[ptr], commit=False)
self.connection.commit()
self.logger.info("Saved %d PTRs to database." % len(ptrs))
def delete_ptrs(self, ip_addresses):
"""
Delete Ptrs for a list of IP addresses
:param ptrs:
:return:
"""
for ip in ip_addresses:
sql = "DELETE FROM `ptrs` WHERE ip_address == :ip"
self.c.execute(sql, {"ip": Ptr.get_ip_int(ip)})
self.connection.commit()
def delete_stale_ptrs(self):
"""
Deletes PTR records that are not seen in more than 72h
:return: Number of deleted rows
"""
stale_hours = self.config['stale_hours'] if 'stale_hours' in self.config else 72
time_threshold = time.time() - stale_hours * 3600
sql = "DELETE FROM `ptrs` WHERE `insert_time` < :time"
self.c.execute(sql, {"time": time_threshold})
return self.c.rowcount
def load_devices(self):
"""
Not used.
:return:
"""
return []