-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherrorlog2syslog
More file actions
executable file
·229 lines (184 loc) · 6.87 KB
/
errorlog2syslog
File metadata and controls
executable file
·229 lines (184 loc) · 6.87 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/python
#
# Ben Beuchler
# <insyte@clockwork.net>
# 04/15/2010
"""\
USAGE: error2syslog [-vh]
Watch FIFODIR for FIFOs with logging data waiting to be read.
Read the data and inject it into syslog.
"""
# Python native modules
import os
import sys
import stat
import time
import select
import optparse
import logging
import logging.handlers
# Third-party modules
import pyinotify
FIFODIR = "/var/spool/apache2/error_logs"
log = logging.getLogger('errlog2syslog')
screenhdlr = logging.StreamHandler()
screenfmt = logging.Formatter('%(levelname)s %(message)s')
screenhdlr.setFormatter(screenfmt)
if sys.stdout.isatty():
log.addHandler(screenhdlr)
filesByName = {}
filesByFd = {}
class InotifyEventHandler(pyinotify.ProcessEvent):
def __init__(self, poller):
self._poller = poller
def process_IN_DELETE(self, event):
if event.path == FIFODIR:
log.debug("Deleted (inotify): %s" % event.name)
try:
fd = filesByName[event.name][0].fileno()
removeFifo(self._poller, fd)
except KeyError:
pass
def process_IN_CREATE(self, event):
if event.path == FIFODIR:
if isFifo(os.path.join(event.path, event.name)):
log.debug("Added (inotify): %s" % event.name)
addFifo(self._poller, os.path.join(FIFODIR, event.name))
def process_default(self, event):
log.debug("Unknown event: %s on %s[%d]" % (event.maskname,
event.path, event.wd))
def isFifo(path):
"""Check to see if the supplied path is a FIFO."""
mode = os.stat(path)[stat.ST_MODE]
return stat.S_ISFIFO(mode)
def watchFifoDir(wm):
"""Try adding the watched directory until it succeeds.
Attempt to create a watch on the requested directory. If it fails, sleep
for 1 second and retry.
"""
inotifyMask = pyinotify.EventsCodes.ALL_FLAGS["IN_DELETE"] | \
pyinotify.EventsCodes.ALL_FLAGS["IN_CREATE"]
watchDir = os.path.dirname(FIFODIR)
while True:
watches = wm.add_watch(watchDir, inotifyMask, rec=True, auto_add=True)
if watches[watchDir] < 0:
log.debug("%s not found." % watchDir)
time.sleep(1)
else:
log.debug("Added watch on: %s" % watchDir)
break
def startInotify(poller):
"""Launch inotify thread.
Watch the FIFO directory and register/unregister FIFOs that are added or
removed. Set the "daemon" flag on the inotify thread so the thread will
automatically terminate when the program exits.
"""
wm = pyinotify.WatchManager()
handler = InotifyEventHandler(poller=poller)
notifier = pyinotify.ThreadedNotifier(wm, handler)
notifier.setDaemon(True)
notifier.start()
watchFifoDir(wm)
def syslogInject(fd):
"""Read the data available on fd and inject into syslog."""
name = filesByFd[fd][1]
log.debug("Reading from %s fd(%d)" % (name, fd))
apacheErr.info("%s[666]: %s" % (filesByFd[fd][1],
filesByFd[fd][0].read().strip()))
def pollFifos(p):
"""Monitor the FIFOs in FIFODIR and process any data written to them.
On startup, grab a list of all the existing FIFOs and register them with
the poll() instance. After that, new additions / removals will be
registered by inotify.
"""
# Register the already existing FIFOs
log.debug("Scanning for existing FIFOs")
c = 0
for f in os.listdir(FIFODIR):
fullpath = os.path.join(FIFODIR, f)
if isFifo(fullpath):
addFifo(p, fullpath)
c += 1
log.debug("Found %d FIFOs." % c)
while True:
# Newly registered FIFOs won't be read until after the next time
# poll() returns, so a timeout is needed.
r = p.poll(5000)
for fdstat in r:
if fdstat[1] & select.POLLIN:
# There is data to be read!!
log.debug("POLLIN: %d" % fdstat[0])
syslogInject(fdstat[0])
if fdstat[1] & select.POLLHUP:
# The other side of the pipe has been closed.
log.debug("POLLHUP: %d" % fdstat[0])
fullpath = os.path.join(FIFODIR, filesByFd[fdstat[0]][1])
removeFifo(p, fdstat[0])
addFifo(p, fullpath)
if fdstat[1] & select.POLLERR:
# An error has occurred. What? We don't know.
log.debug("POLLERR: %d" % fdstat[0])
def addFifo(poller, fullpath):
"""Add a FIFO to the poller object and save the info."""
fifo = os.fdopen(os.open(fullpath, os.O_NONBLOCK))
fd = fifo.fileno()
fn = os.path.basename(fullpath)
filesByName[fn] = (fifo, fn)
filesByFd[fd] = (fifo, fn)
log.debug("Adding FIFO to poller: %s[%d]" % (fullpath, fd))
poller.register(fd, select.POLLIN)
def removeFifo(poller, fd):
"""Remove a FIFO from the poller object."""
fn = filesByFd[fd][1]
log.debug("Removing FIFO from poller: %s[%d]" % (fn, fd))
poller.unregister(fd)
del(filesByName[fn])
del(filesByFd[fd])
def apacheLoggingSetup():
"""Setup Apache Logging via /dev/log"""
global apacheErr
apacheErr = logging.getLogger('apache_error_log')
while True:
try:
apacheErrHdlr = logging.handlers.SysLogHandler('/dev/log',
'local2')
break
except IOError as (errno, strerror):
log.debug("Unable to connect to /dev/log: [Errno {0}] {1}"
.format(errno, strerror))
time.sleep(1)
except:
log.debug("Unable to connect to /dev/log: %s" % sys.exc_info()[0])
time.sleep(1)
apacheErrFmt = logging.Formatter('%(message)s')
apacheErrHdlr.setFormatter(apacheErrFmt)
apacheErr.addHandler(apacheErrHdlr)
apacheDebugHdlr = logging.StreamHandler()
apacheDebugFmt = logging.Formatter('%(message)s')
apacheDebugHdlr.setFormatter(apacheDebugFmt)
apacheErr.setLevel(logging.DEBUG)
if sys.stdout.isatty():
apacheErr.addHandler(apacheDebugHdlr)
def parserSetup():
"""Setup the OptionParser instance."""
p = optparse.OptionParser(__doc__)
p.add_option("--verbose", "-v", action="store_true",
help="Print copious debugging info. "
"Only used when run from a terminal.")
p.add_option("--fifodir", "-d", action="store", default=FIFODIR,
help="Directory to scan for Apache's logging FIFOs. "
"Defaults to: %s" % FIFODIR)
return p
def main(argv):
global FIFODIR
p = parserSetup()
opts, args = p.parse_args(argv)
FIFODIR = opts.fifodir
if opts.verbose:
log.setLevel(logging.DEBUG)
apacheLoggingSetup()
poller = select.poll()
startInotify(poller)
pollFifos(poller)
if __name__ == "__main__":
main(sys.argv[1:])