3 # Plugin to monitor the types of requsts made to the API
10 # autoconf (optional - used by munin-config)
16 from datetime import datetime, timedelta
18 CONFIG="""graph_title Requests by API call
19 graph_args --base 1000 -l 0
20 graph_vlabel requests per minute
21 graph_category nominatim
25 z2.label search (successful)
28 z3.label search (no result)
35 ENTRY_REGEX = re.compile(r'\[[^]]+\] (?P<dur>[0-9.]+) (?P<numres>\d+) (?P<type>[a-z]+) ')
36 TIME_REGEX = re.compile(r'\[(?P<t_year>\d\d\d\d)-(?P<t_month>\d\d)-(?P<t_day>\d\d) (?P<t_hour>\d\d):(?P<t_min>\d\d):(?P<t_sec>\d\d)[0-9.]*\] ')
40 """ A query log file, unpacked. """
42 def __init__(self, filename):
43 self.fd = open(filename, encoding='utf-8', errors='replace')
44 self.len = os.path.getsize(filename)
49 def seek_next(self, abstime):
52 l = self.fd.readline()
53 e = TIME_REGEX.match(l)
57 return datetime(int(e['t_year']), int(e['t_month']), int(e['t_day']),
58 int(e['t_hour']), int(e['t_min']), int(e['t_sec']))
60 def seek_to_date(self, target):
61 # start position for binary search
63 fromdate = self.seek_next(0)
66 # end position for binary search
68 while -toseek < self.len:
69 todate = self.seek_next(self.len + toseek)
70 if todate is not None:
73 if todate is None or todate < target:
75 toseek = self.len + toseek
79 bps = (toseek - fromseek) / (todate - fromdate).total_seconds()
80 newseek = fromseek + int((target - fromdate).total_seconds() * bps)
81 newdate = self.seek_next(newseek)
84 error = abs((target - newdate).total_seconds())
90 oldfromseek = fromseek
91 fromseek = toseek - error * bps
93 if fromseek <= oldfromseek:
94 fromseek = oldfromseek
95 fromdate = self.seek_next(fromseek)
97 fromdate = self.seek_next(fromseek)
101 fromseek -= error * bps
106 toseek = fromseek + error * bps
108 if toseek > oldtoseek:
110 todate = self.seek_next(toseek)
112 todate = self.seek_next(toseek)
116 toseek += error * bps
117 if toseek - fromseek < 500:
123 e = ENTRY_REGEX.match(l)
128 if __name__ == '__main__':
130 if len(sys.argv) > 1 and sys.argv[1] == 'config':
138 if 'NOMINATIM_QUERYLOG' in os.environ:
139 lf = LogFile(os.environ['NOMINATIM_QUERYLOG'])
140 if lf.seek_to_date(datetime.now() - timedelta(minutes=5)):
141 for l in lf.loglines():
142 if l['type'] == 'reverse':
144 elif l['type'] == 'search':
145 if l['numres'] == '0':
153 print('z1.value', reverse/5)
154 print('z2.value', searchy/5)
155 print('z3.value', searchn/5)
156 print('z4.value', details/5)