/usr/share/petit/crunchtools/ScriptLog.py is in petit 1.1.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 230 231 232 233 234 235 236 237 238 239 240 241 | """Defines ScriptLog class"""
from UserList import UserList
from CrunchLog import ScriptlogEntry
import re
import sys
import os
import logging
import gzip
import sha
import random
import syslog
class ScriptLog(UserList):
"""Class which allows special use cases for dealing with Report/Acknowledge"""
# Variables
filename = ""
label = ""
warn = {}
crit = {}
ack = {}
# Labels
labels = {}
labels["warn"] = "__WARN__"
labels["crit"] = "__CRIT__"
labels["ack"] = "__ACKN__"
def __init__(self, filename, label="__ScriptLog__", auto_refresh=False):
# Initialize variables
self.filename = filename
self.label = label
self.auto_refresh = auto_refresh
self.fill()
def open_file(self, filename):
"""Helper function used to open all logs including previously rotated logs"""
global logging
# Create new array to hold log data
buffer = []
trimfiles = []
# Check for empty, logfile, or read stdin
if filename == "":
return
elif filename == "__none__":
f = sys.stdin
else:
# Search the directory for all files
dirname = os.path.dirname(filename)
basename = os.path.basename(filename)
files = os.listdir(os.path.dirname(filename))
# Trim files down to ones that make sense
for file in files:
if re.search(basename, file, re.IGNORECASE):
# Open the file
logging.debug("Opening File: "+dirname+"/"+file)
# Check for zip file
if re.search("gz", file):
f = gzip.open(dirname+"/"+file)
else:
f = open(dirname+"/"+file)
# Read entire contents into array for speed
for line in f.readlines():
buffer.append(line)
# Close file
f.close();
return buffer
def fill(self):
"""Separate method used to refresh the ScriptLog data structure from file"""
# Clean up all data structures for each fill
UserList.__init__(self)
buffer = ()
self.warn = {}
self.crit = {}
self.ack = {}
# Create new array to hold log data
if os.path.exists(self.filename):
buffer = self.open_file(self.filename)
else:
print "File does not exist:", self.filename
sys.exit(16)
# Buffer has bow been created and work with file is done
# Now it is time to determine what kind of objects will be
# used for construction of self.
# Finally, build self with the ScriptlogEntries
for line in buffer:
# Test line to make sure it is a ScriptLog entry, then add
if ScriptlogEntry.is_type(line, self.label):
# Setup main list (self)
self.append(ScriptlogEntry(line.expandtabs()))
# Setup supporting dictionaries
# Set Entry
entry = self[len(self)-1]
# Parse warning items
if entry.type == ScriptLog.labels["warn"]:
self.warn[entry.id] = True
# Parse critical items
if entry.type == ScriptLog.labels["crit"]:
self.crit[entry.id] = True
# If a genuine ack is found, increment
if entry.type == ScriptLog.labels["ack"]:
self.ack[entry.id] = True
def has_entry(self, line):
"""Check the scriptlog object for an entry which matches the line given"""
# Always check before adding the entry
if self.auto_refresh:
self.fill()
for entry in self:
# Strip extra spaces out
line = re.sub("\s+", " ", line)
# Complete the search
#if re.search(re.escape(line), entry.log_entry, re.IGNORECASE):
if line == entry.log_entry:
return True
# Default
return False
def add_warn(self, line):
"""Added warning entry to syslog"""
# Generate new SHA has
h = sha.new(str(random.random())+self.label+" "+ScriptLog.labels["warn"]+" "+line)
# Write out entry
syslog.syslog(self.label+" "+h.hexdigest()+" "+ScriptLog.labels["warn"]+" "+line.expandtabs())
# Refresh the log
if self.auto_refresh:
self.fill()
def add_crit(self, line):
"""Added critical entry to syslog"""
# Generate new SHA has
h = sha.new(str(random.random())+self.label+" "+ScriptLog.labels["warn"]+" "+line)
# Write out entry
syslog.syslog(self.label+" "+ScriptLog.labels["crit"]+" "+line)
# Refresh the log
if self.auto_refresh:
self.fill()
def add_ack(self, id):
"""Added critical entry to syslog"""
# Write out entry
syslog.syslog(self.label+" "+id+" "+ScriptLog.labels["ack"])
# Refresh the log
if self.auto_refresh:
self.fill()
def show_unacknowledged(self):
"""Display function commonly used in derivative works"""
# Create a list of items
unack = {}
RETVAL = 0
# Reload all data
self.fill()
# Build up a list of unacknowledged entries and associated count
# Then use that list to display all information from the syslog entries
# Iterate all warn entries and increment
for k in self.warn:
if k not in self.ack:
unack[k] = True
RETVAL = 1
for k in self.crit:
if k not in self.ack:
unack[k] = True
RETVAL = 2
if len(unack) > 0:
print "Unacknowledged Items:"
# Iterate full entries so that all information can be printed
for entry in self:
if entry.id in unack:
print entry.month \
+" "+entry.day \
+" "+entry.hour+":"+entry.minute+":"+entry.second \
+" "+entry.type \
+" \""+entry.log_entry+"\""
else:
print "OK"
return RETVAL
def acknowledge_all(self):
"""Acknowledge all entries which have not otherwise been acknowledged"""
global logging
for k in self.warn:
if k not in self.ack:
self.add_ack(k)
for k in self.crit:
if k not in self.ack:
self.add_ack(k)
# Then reload all data
self.fill()
|