Subversion Repositories alertlog

Rev

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
#
4
# alertlog.py
2 t00mlabs 5
# Copyright (C) 2013 Tomás Vírseda <tomasvirseda@gmail.com>
1 t00mlabs 6
#
7
# alertlog is free software: you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by the
9
# Free Software Foundation, either version 3 of the License, or
10
# (at your option) any later version.
11
#
2 t00mlabs 12
# alertlog is distributed in the hope that it will be useful, but
1 t00mlabs 13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15
# See the GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License along
18
# with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
20
import sys
21
import gi
22
gi.require_version('Gtk', '3.0')
23
from gi.repository import Gtk, GdkPixbuf, Gdk
24
 
25
# With Python2
26
#~ from BeautifulSoup import BeautifulStoneSoup
27
 
28
# With Python3
29
from bs4 import BeautifulStoneSoup
30
 
31
#FIXME
32
#UI_FILE = "alertlog.ui" # Development
33
UI_FILE = "/usr/share/alertlog/ui/alertlog.ui" #Production
34
HEADERS = ['time', 'org_id', 'comp_id', 'client_id', 'type', \
35
                'level', 'host_id', 'host_addr', 'module', 'pid']
36
 
37
class GUI:
38
    def __init__(self):
39
        self.total = 0
40
        self.dlog = {}
41
        self.progress = 0
42
        self.builder = Gtk.Builder()
43
        self.builder.add_from_file(UI_FILE)
44
        self.builder.connect_signals(self)
45
 
46
        self.cfilter = self.builder.get_object('cbtFilter')
47
        self.tfilter = self.builder.get_object('etyFilter')
48
        self.filechooser = self.builder.get_object('fcbLog')
49
        self.window = self.builder.get_object('window')
50
        self.messages = self.builder.get_object('scwMessages')
51
        self.text = self.builder.get_object('txvMessage')
52
        self.__setup_gui()
53
        self.window.show_all()
54
 
55
 
56
    def __setup_gui(self):
57
        # setup messages model & widgets
58
        self.store = Gtk.ListStore(str, str, str, str, str, str, \
59
                                str, str, str, str, str)
60
 
61
        tree = Gtk.TreeView(self.store)
62
        tree.connect("cursor-changed", self.on_node_changed)
63
        renderer = Gtk.CellRendererText()
64
        column = Gtk.TreeViewColumn('Id', renderer, text=0)
65
        #~ column.set_visible(False)
66
        tree.append_column(column)
67
        n = 1
68
        for field in HEADERS:
69
            renderer = Gtk.CellRendererText()
70
            column = Gtk.TreeViewColumn(field, renderer, text=n)
71
            column.set_sort_column_id(n)
72
            column.set_expand(True)
73
            tree.append_column(column)
74
            n = n + 1
75
 
76
        tree.show_all()
77
        self.messages.add(tree)
78
 
79
        # setup comboboxtext filter
80
        for key in HEADERS:
81
            self.cfilter.append_text(key)
82
        self.cfilter.append_text('txt')
83
        self.cfilter.set_active(0)
84
 
85
 
86
    def __len__(self):
87
        return len(self.dlog)
88
 
89
 
90
    def file_set(self, filechooser):
91
        filename = filechooser.get_filename()
92
        self.analyze(filename)
93
        self.update()
94
 
95
 
96
    def filter_activated (self, data):
97
        daux = {}
98
        text = self.tfilter.get_text()
99
        filter = self.cfilter.get_active_text()
100
        for node in xrange(len(self.dlog)):
101
            try:
102
                if text.upper() in self.dlog[node][filter].upper():
103
                    daux[node] = self.dlog[node]
104
            except:
105
                pass
106
        self.update(daux, True)
107
 
108
 
109
    def filter_changed (self, data):
110
        # callback not used.
111
        pass
112
 
113
 
114
    def destroy(self, window, event):
115
        Gtk.main_quit()
116
 
117
 
118
    def update(self, daux={}, filter=False):
119
        self.store.clear()
120
 
121
        if not filter:
122
            daux = self.dlog.copy()
123
 
124
        nodelist = []
125
        for node in daux:
126
            nodelist.append(node)
127
        nodelist.sort()
128
 
129
        for node in nodelist:
130
            row = []
131
            row.append(str(node))
132
            for key in HEADERS:
133
                try:
134
                    if key == 'time':
135
                        time = daux[node][key]
136
                        date = time[:10] + ' ' + time[11:23]
137
                        row.append(date)
138
                    else:
139
                        row.append(daux[node][key])
140
                except:
141
                    # messages do not have all fields
142
                    row.append("")
143
            self.store.append(row)
144
 
145
 
146
    def on_node_changed (self, treeview):
147
        try:
148
            selection = treeview.get_selection()
149
            result = selection.get_selected()
150
            if result:
151
                model, treeiter = result
152
                node = model[treeiter][0]
153
                text = self.dlog[int(node)]['txt']
154
                buffer = self.text.get_buffer()
155
                buffer.set_text(text)
156
                self.text.set_buffer(buffer)
157
        except: pass
158
 
159
 
160
    def analyze(self, filename):
161
        self.total = 0
162
        self.progress = 0
163
        log = open(filename, 'r').read()
164
        soup = BeautifulStoneSoup(log)
165
        messages = soup.findAll('msg')
166
        n = 0
167
        for node in messages:
168
            self.dlog[n] = {}
169
            for key, value in node.attrs:
170
                if key != 'version':
171
                    self.dlog[n][key] = value
172
            self.dlog[n]['txt'] = node.txt.text
173
            n = n + 1
174
 
175
 
176
def main():
177
    app = GUI()
178
    Gtk.main()
179
 
180
 
181
if __name__ == "__main__":
182
    sys.exit(main())