Menu

[r8622]: / trunk / gui / tools / alert.py  Maximize  Restore  History

Download this file

140 lines (120 with data), 5.0 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
#!/usr/bin/env python
#-
# Copyright (c) 2011 iXsystems, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
from cStringIO import StringIO
import hashlib
import os
import sys
sys.path.extend([
'/usr/local/www',
'/usr/local/www/freenasUI'
])
from freenasUI import settings
from django.core.management import setup_environ
setup_environ(settings)
from django.contrib.auth.models import User, UNUSABLE_PASSWORD
from django.utils.translation import ugettext_lazy as _
from freenasUI.common.system import send_mail
from freenasUI.storage.models import Volume
from freenasUI.system.models import Settings
ALERT_FILE = '/var/tmp/alert'
LAST_ALERT_FILE = '/var/tmp/alert.last'
class Alert(object):
LOG_OK = "OK"
LOG_CRIT = "CRIT"
LOG_WARN = "WARN"
def __init__(self):
self.__s = StringIO()
self.__logs = {
self.LOG_OK: [],
self.LOG_CRIT: [],
self.LOG_WARN: [],
}
def log(self, level, msg):
msg = unicode(msg)
self.__logs[level].append(msg)
self.__s.write('%s: %s\n' % (level, msg, ))
def volumes_status(self):
for vol in Volume.objects.filter(vol_fstype__in=['ZFS', 'UFS']):
if vol.status == 'HEALTHY':
self.log(self.LOG_OK,
_('The volume %s status is HEALTHY') % (vol, ))
elif vol.status == 'DEGRADED':
self.log(self.LOG_CRIT,
_('The volume %s status is DEGRADED') % (vol, ))
else:
self.log(self.LOG_WARN,
_('The volume %(volume)s status is %(status)s') % {'volume': vol, 'status': vol.status})
def admin_password(self):
user = User.objects.filter(password=UNUSABLE_PASSWORD)
if user.exists():
self.log(self.LOG_CRIT, _('You have to change the password for '
'the admin user (currently no password '
'is required to login)'))
def lighttpd_bindaddr(self):
address = Settings.objects.all().order_by('-id')[0].stg_guiaddress
with open('/usr/local/etc/lighttpd/lighttpd.conf') as f:
# XXX: this is parse the file instead of slurping in the contents
# (or in reality, just be moved somewhere else).
if f.read().find('0.0.0.0') != -1 and address not in ('0.0.0.0', ''):
# XXX: IPv6
self.log(self.LOG_WARN,
_('The WebGUI Address could not be bind to %s; using wildcard')
% (address,))
def perform(self):
self.volumes_status()
self.admin_password()
self.lighttpd_bindaddr()
def write(self):
with open(ALERT_FILE, 'w') as f:
f.write(self.__s.getvalue())
def email(self):
"""
Use alert.last to hold a sha256 hash of the last sent alerts
If the hash is the same do not resend the email
"""
if len(self.__logs[self.LOG_CRIT]) == 0:
if os.path.exists(LAST_ALERT_FILE):
os.unlink(LAST_ALERT_FILE)
return
try:
with open(LAST_ALERT_FILE) as f:
sha256 = f.read()
except:
sha256 = ''
newsha = hashlib.sha256(repr(self.__logs[self.LOG_CRIT])).hexdigest()
if newsha != sha256:
send_mail(subject=_("Critical Alerts"),
text='\n'.join(self.__logs[self.LOG_CRIT]))
with open(LAST_ALERT_FILE, 'w') as f:
f.write(newsha)
def __del__(self):
self.__s.close()
if __name__ == '__main__':
alert = Alert()
alert.perform()
alert.email()
alert.write()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.