summaryrefslogtreecommitdiffstats
path: root/logman.py
blob: 05ca191c10f094236d25f3ee80e35e5dbc235660 (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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python

from subprocess import call
import sys
import subprocess
import dbus
import string
import socket
import os
import fcntl
import time
import glib
import gobject
import dbus.service
import dbus.mainloop.glib

DBUS_NAME = 'org.openbmc.LogManager'
ERRL_INTF_NAME = 'org.openbmc.Errl'
SRVC_INTF_NAME = 'org.openbmc.Service'
OBJ_NAME_RSYSLOG = '/org/openbmc/LogManager/rsyslog'

'''
    Object Path > /org/openbmc/LogManager/rsyslog
        Interface:Method > org.openbmc.Service.Enable dict:string:string
        Interface:Method > org.openbmc.Service.Disable
'''

class JournalUtils ():
    def _isvalidip(self, family, ipaddr):
        if family == socket.AF_INET:
            try:
                socket.inet_pton(socket.AF_INET, ipaddr)
            except AttributeError:  # no inet_pton here, sorry
                try:
                    socket.inet_aton(ipaddr)
                except socket.error:
                    return False
                return ipaddr.count('.') == 3
            except socket.error:  # not a valid address
                return False

            return True

        elif family == socket.AF_INET6:
            try:
                socket.inet_pton(socket.AF_INET6, ipaddr)
            except socket.error:  # not a valid address
                return False
            return True

        else: return False

class Rsyslog (dbus.service.Object):
    def __init__(self, bus, name):
        self.bus = bus
        self.name = name
        dbus.service.Object.__init__(self,bus,name)

    @dbus.service.method(dbus.PROPERTIES_IFACE, "ss", "v")
    def Get(self, iface, ppty):
        return self.GetAll(iface)[ppty]

    @dbus.service.method(dbus.PROPERTIES_IFACE, 's', 'a{sv}')
    def GetAll(self, iface):
        if iface == ERRL_INTF_NAME:
            status, remote_ip, remote_port = self.Status()
            return {'status': status, 'ipaddr': remote_ip, 'port': remote_port }
        else:
            raise dbus.exceptions.DBusException('org.openbmc.UnknownInterface',
                                'This object does not implement the %s interface' % iface)

    @dbus.service.method(SRVC_INTF_NAME, "a{sv}", "x")
    def Enable (self, argv_dict):
        remote_ip = ""
        remote_port = 0

        params = len (argv_dict)
        if params > 2 : ValueError("Invalid Parameters")

        for property_name in argv_dict:
            if property_name == "ipaddr":
                remote_ip = argv_dict [property_name]
            elif property_name == "port":
                remote_port = argv_dict [property_name]
            else: 
                raise ValueError("Invalid Argument: IP Address/Port expected.")

        if not remote_ip: 
            cur_remote = self._GetConfig ('Remote')
            if not cur_remote: 
                raise ValueError("Invalid Remote Syslog IP Address")
            else:
                cur_remote = cur_remote[3:]
                remote_ip, port_str = cur_remote.split (":")
                remote_port = int(port_str)
        if not util._isvalidip (socket.AF_INET, remote_ip): raise ValueError, "Malformed IP Address"
        if not remote_port : remote_port = 514
        if remote_port > 65535 : raise ValueError("Invalid Remote Syslog Port")
        
        remote_addr = remote_ip + ":" + str(remote_port)
        r = self._ModifyService('Remote', remote_addr)

        cur_options = self._GetConfig ('Options')
        new_options = self._GetOptions()

        if cur_options != new_options:
            r = self._ModifyService('Options', new_options)
            r = self._RestartService ()

        return r

    @dbus.service.method(SRVC_INTF_NAME, "as", "x")
    def Disable (self, argv_list):
        params = len (argv_list)
        if params : ValueError("Invalid Parameters")

        remote = self._GetConfig ('Remote')
        if not remote : return 0

        r = self._ModifyService('Options', '-C') # FIXME: Restore current options minus the remote.
        r = self._RestartService ()
        return r

    def Status (self):
        remote = self._GetConfig ('Remote')
        if not remote : return ("Disabled", "0.0.0.0", 0)

        cur_remote = remote[3:]
        remote_ip, remote_port = cur_remote.split (":")
        
        options = self._GetConfig ('Options')
        if not options : return ("Disabled", remote_ip, remote_port)

        if remote in options : return ("Enabled", remote_ip, remote_port)

        return ("Disabled", remote_ip, remote_port)

    def _ModifyService (self, opt, val):
        if not os.path.isfile(syslog_service_bbx_file):
            r = call (["cp", syslog_service_lib_file, syslog_service_bbx_file])
            r = call (["ln", "-s", syslog_service_bbx_file, syslog_service_cfg_file])

        if not os.path.isfile(syslog_service_env_file):
            env_file = open(syslog_service_env_file, 'w')
            env_file.write ("OPTIONS=\"-C\"")
            env_file.close()

        if opt not in OptionKeys: raise ValueError("Invalid Option")

        self._ModifyParam (opt, val)

        return 0

    def _StopService (self):
        r = call (["systemctl", "stop", "syslog"])
        r = call (["systemctl", "--no-reload", "kill", "syslog"])
        return r

    def _StartService (self):
        r = call (["systemctl", "daemon-reload"])
        r = call (["systemctl", "start", "syslog"])
        return r

    def _RestartService (self):
        r = self._StopService()
        r = self._StartService()
        return r
    
    def _ModifyParam (self, opt, val):
        env_file = open(syslog_service_env_file, 'r') 
        tmp_file = open(syslog_service_tmp_file, 'w')

        optkey = OptionKeySwitchMap [opt]['key']
        for line in env_file:
            if line[0] == '#': 
                tmp_file.write(line)
                continue
            curkey = line.strip().split ("=")[0]
            if curkey != optkey : 
                tmp_file.write(line)

        tmp_file.write(optkey + "=\""  + OptionKeySwitchMap[opt]['switch'] + val + "\"" + "\n")

        env_file.close ()
        tmp_file.close ()

        r = call (["cp", syslog_service_tmp_file, syslog_service_env_file])
        return r

    def _GetConfig (self, opt):
        with open(syslog_service_env_file, "r") as f:
            for line in f:
                if line[0] == '#': continue
                config = line.split ("=")
                var = config [0]
                if var == OptionKeySwitchMap[opt]['key']:
                    val = config [1]
                    val = val[1:-2] # FIXME: Why is there a trailing space ???
                    return val
        return ""

    def _GetOptions(self):
        cfg = {}
        i = 0

        for opt in OptionKeys:
            if opt == 'Options' : continue
            cfg [i] = self._GetConfig(opt)
            i+=1
            
        options = ''
        j = 0
        while j<i-1:
            if cfg[j] : options += cfg [j]
            j+=1

        return options

def main():
    dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
    bus = dbus.SystemBus()
    name = dbus.service.BusName(DBUS_NAME, bus)

    global util
    global rsys
    global syslog_service_lib_file
    global syslog_service_bbx_file
    global syslog_service_cfg_file
    global syslog_service_env_file
    global syslog_service_tmp_file
    global OptionKeys
    global OptionKeySwitchMap

    OptionKeys = ['Options', 'Outfile', 'Priority', 'Smaller', 'RotateSize', 'RotateNum', 'Remote', 'LocalAndNet', 'DropDup', 'SharedMem', 'ConfFile', 'MarkTime', 'Printk']
    OptionKeySwitchMap = {
        'Options'       : { 'switch' : "",    'key' : "OPTIONS" },
        'Outfile'       : { 'switch' : "-O ", 'key' : "OBMC_SYSLOG_OUTFILE" },
        'Priority'      : { 'switch' : "-O ", 'key' : "OBMC_SYSLOG_PRIORITY" },
        'Smaller'       : { 'switch' : "-S ", 'key' : "OBMC_SYSLOG_SMALLER" },
        'RotateSize'    : { 'switch' : "-s ", 'key' : "OBMC_SYSLOG_ROTATESIZE" },
        'RotateNum'     : { 'switch' : "-b ", 'key' : "OBMC_SYSLOG_ROTATENUM" },
        'Remote'        : { 'switch' : "-R ", 'key' : "OBMC_SYSLOG_REMOTE" },
        'LocalAndNet'   : { 'switch' : "-L ", 'key' : "OBMC_SYSLOG_LOCALNET" },
        'DropDup'       : { 'switch' : "-D ", 'key' : "OBMC_SYSLOG_DROPDUP" },
        'SharedMem'     : { 'switch' : "-C ", 'key' : "OBMC_SYSLOG_SHAREDMEM" },
        'ConfFile'      : { 'switch' : "-f ", 'key' : "OBMC_SYSLOG_CONFFILE" },
        'MarkTime'      : { 'switch' : "-m ", 'key' : "OBMC_SYSLOG_MARKTIME" },
        'Printk'        : { 'switch' : "-K ", 'key' : "OBMC_SYSLOG_PRINTK" }
    }

    syslog_service_lib_file = '/lib/systemd/system/busybox-syslog.service'
    syslog_service_bbx_file = '/etc/systemd/system/busybox-syslog.service'
    syslog_service_cfg_file = '/etc/systemd/system/syslog.service'
    syslog_service_env_file = '/etc/default/busybox-syslog'
    syslog_service_tmp_file = '/tmp/busybox-syslog.tmp'

    util    = JournalUtils ()
    rsys    = Rsyslog (bus, OBJ_NAME_RSYSLOG)

    mainloop = gobject.MainLoop()
    print("Started")
    mainloop.run()

if __name__ == '__main__':
    sys.exit(main())

OpenPOWER on IntegriCloud