-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcollect.py
executable file
·153 lines (124 loc) · 4.17 KB
/
collect.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
#!/usr/bin/env python
"""
Collect: Collect Duo logs
"""
import argparse
import os
import re
import sys
import time
import traceback
from threading import Thread, Event
from time import localtime, strftime
import argus_daemon
import duo_watcher
def looper(tp):
"""
Thread to loop forever scarfing up Duo log messages, yum, yum
"""
print('{ts} {pid}: Thread {name} starting.'.format(
ts = time.strftime('%y-%m-%d %H:%M:%S'),
pid = os.getpid(),
name = tp.name))
sys.stdout.flush()
while not tp.terminate.isSet():
while not tp.terminate.isSet():
tp.timestamp = time.time()
result = tp.handle.fetch()
tp.count = tp.count + 1
t0 = strftime('%H:%M:%S', localtime(tp.timestamp))
t1 = strftime('%y-%m-%d %H:%M:%S', localtime(tp.handle.state['timestamp']))
tp.status = 'At {wall} up to {log} count: {count} interval: {val}'.format(wall=t0, log=t1, count=tp.count, val=tp.interval)
if tp.handle.backoff > 0:
tp.status = tp.status + '+{bo}'.format(bo=tp.handle.backoff)
if not result:
break
tp.terminate.wait(tp.interval + tp.handle.backoff)
if tp.maxcount > 0 and tp.count > tp.maxcount:
break
tp.status = 'Stopped ' + tp.status
print('{ts} {pid}: Thread {name} terminating.'.format(
ts = time.strftime('%y-%m-%d %H:%M:%S'),
pid = os.getpid(),
name = tp.name))
sys.stdout.flush()
#
# Initialize our thread descriptions
#
threads = [
argus_daemon.Argus_thread('auth', 'authentication', auto = True, target = looper),
argus_daemon.Argus_thread('admin', 'administrator', auto = True, target = looper),
argus_daemon.Argus_thread('phone', 'telephony', auto = True, target = looper)
]
#
# Start the Argus listener and become a proper daemon
#
argus = argus_daemon.Argus(threads)
def main():
ap = argparse.ArgumentParser(description='Collect Duo Logs')
ap.add_argument('-d', action='store_true', help='Become a deamon')
arg = ap.parse_args()
if arg.d:
argus.deamonize()
print('{ts} {pid}: Duo collection daemon starting...'.format(
ts = time.strftime('%y-%m-%d %H:%M:%S'),
pid = os.getpid()))
sys.stdout.flush()
for tp in threads:
tp.handle = duo_watcher.LogWatcher(tp.name, tp.resource)
if tp.auto:
try:
tp.thread = Thread(target=looper, args=(tp,))
tp.thread.start()
except Exception as errtxt:
tp.alert = 8
tp.status = errtxt
else:
tp.active = True
#
# Enter the main loop
#
while True:
try:
line = argus.getMessage()
except KeyboardInterrupt:
print('')
break
except:
print('Exiting loop because of unhandled exception')
traceback.print_exc()
break
if not line:
break
answer = 'P5Unrecognized command'
if line == 'help':
answer = ('P3Help yourself\n\n' +
'Commands are:\n' +
' clear: Clear status\n' +
' status: Report status\n' +
' rotate: Logfile rotation\n' +
' thread {name} start\n' +
' thread {name} stop\n' +
' thread {name} interval {seconds}\n' +
' thread {name} maxcount {count}\n' +
'Threads are:\n')
for tp in threads:
answer = answer + ' {name}: Duo {resource} log watcher\n'.format(name = tp.name, resource = tp.resource)
argus.sendResponse(answer)
#
# Clean up after termination
#
print('Exiting main loop.')
sys.stdout.flush()
for tp in threads:
if tp.active:
tp.terminate.set()
for tp in threads:
if tp.active:
print('Joining thread for {name}'.format(name = tp.name))
sys.stdout.flush()
tp.thread.join(10.0)
tp.active = False
tp.thread = None
if __name__ == '__main__':
main()