aboutsummaryrefslogtreecommitdiffstats
path: root/src/msec/tools.py
blob: cb9d1bf1844d7f2f577db0623d4fc6d2d5d90344 (plain)
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
#!/usr/bin/python
#
# msec: helper tools
#

import os
import stat
import sys
import time
import locale

from config import Narg, SYS_ENC

# localization
import gettext
try:
    gettext.install("msec", unicode=1)
except IOError:
    _ = str

# constants
FIREWALL_CMD = "drakfirewall &"
UPDATE_CMD = "MageiaUpdate &"

def find_firewall_info(log):
    """Finds information about firewall"""
    # read firewall settings
    firewall_entries = []
    try:
        data = os.popen("iptables -S").readlines()
        for l in data:
            if l[:3] == "-A ":
                firewall_entries.append(l.strip())
    except:
        log.error(_("Unable to parse firewall configuration: %s") % Narg(sys.exc_value))
    # not find out if the firewall is enabled
    if len(firewall_entries) == 0:
        firewall_status = _("Disabled")
    else:
        firewall_status = _("Enabled, with %d rules") % len(firewall_entries)
    return firewall_status

def get_updates_status(log, updatedir="/var/lib/urpmi"):
    """Get current update status"""
    # just find out the modification time of /var/lib/urpmi
    try:
        ret = os.stat(updatedir)
        updated = time.localtime(ret[stat.ST_MTIME])
        status = _("Last updated: %s") % Narg(updated_s)
    except:
        log.error(_("Unable to access %s: %s") % (updatedir, Narg(sys.exc_value)))
        status = _("Unable to determine update status")
    return status

def periodic_check_status(log):
    """Determines the state of last periodic checks"""
    checks = []
    for check in [ "daily", "weekly", "monthly", "manual" ]:
        current_log = "/var/log/security/mail.%s.today" % check
        if os.access(current_log, os.R_OK):
            ret = os.stat(current_log)
            last_run = time.localtime(ret[stat.ST_MTIME])
            updated_s = time.strftime(locale.nl_langinfo(locale.D_T_FMT), last_run)
            checks.append((check, current_log, last_run, updated_s))
        else:
            checks.append((check, current_log, None, None))
    return checks