Projet

Général

Profil

Révision 167c204d

ID167c204db263c9bddffb790cd84da66fa2ab5123
Parent 051ee8ea
Enfant 890e971c

Ajouté par Felix Pahlow il y a plus de 7 ans

Munin ILIAS plugin: First release

Voir les différences:

plugins/ilias/ilias_
1
#!/usr/bin/env python
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
2 3

  
3
# The Plugin needs the following configuration settings
4
#[ilias_*]
5
#env.ildbuser ilias
6
#env.ildbpassword youriliaspasword
7
#env.ildb ilias
4
"""
5
: << =cut
8 6

  
9
#%# family=auto
10
#%# capabilities=autoconf suggest
7
=head1 NAME
8

  
9
ilias - Munin plugin to monitor L<ILIAS|https://ilias.de/> open source
10
learning management system
11

  
12
=head1 DESCRIPTION
13

  
14
Reads session and user statistcs from any ILIAS MySQLdb database.
15

  
16
https://ilias.de/ | http://gallery.munin-monitoring.org/contrib/cms-index.html
17

  
18
This plugin requires python3 and python3-mysqldb.
19

  
20
There is a check for the the filename suffix  _ (from the symlink) in place
21
to decide which value to output. Symlink the file for each value you want
22
displayed
23
example:
24
    ln -s /usr/local/munin_plugins/ilias_ /etc/munin/plugins/ilias_sessions
25

  
26
In order to get precise results, please ensure your MySQL server has the same
27
time as your ILIAS application server. Timezone does not matter.
28

  
29
=head1 CONFIGURATION
30

  
31
The plugin needs the following configuration settings e.g. in
32
/etc/munin/plugin-conf.d/ilias.conf
33

  
34
    [ilias_*]
35
        env.ildbuser ilias
36
        env.ildbpassword youriliaspasword
37
        env.ildb ilias
38
        env.ildbhost localhost
39
        env.ildbport 3306
40

  
41

  
42
=head1 AUTHOR
43

  
44
Copyright 2016 Pascal Seeland <per-pascal.grube@tik.uni-stuttgart.de>
45

  
46
Copyright 2018 L<Felix Pahlow|https://wohlpa.de/>
47
               (L<email|mailto:felix.pahlow@itz.uni-halle.de>)
48

  
49
=head1 LICENSE
50

  
51
Permission is hereby granted, free of charge, to any person obtaining a copy
52
of this software and associated documentation files (the "Software"), to deal
53
in the Software without restriction, including without limitation the rights
54
to use, copy, modify, merge, publish, distribute, and/or sell copies of the
55
Software, and to permit persons to whom the Software is furnished to do so,
56
provided that the above copyright notice(s) and this permission notice
57
appear in all copies of the Software and that both the above copyright
58
notice(s) and this permission notice appear in supporting documentation.
59

  
60
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
61
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
62
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
63
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE BE
64
LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR
65
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
66
IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
67
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
68

  
69
Except as contained in this notice, the name of a copyright holder shall not
70
be used in advertising or otherwise to promote the sale, use or other
71
dealings in this Software without prior written authorization of the
72
copyright holder.
73

  
74
=head1 CONTRIBUTE
75

  
76
Find this plugin on L<GitHub
77
|https://github.com/munin-monitoring/contrib/tree/master/plugins/ilias>
78

  
79
=head1 MAGIC MARKERS
80

  
81
 #%# family=auto
82
 #%# capabilities=autoconf suggest
83

  
84
=head1 VERSION
85

  
86
    1.0
87

  
88
=head1 CHANGELOG
89

  
90
=head2 1.0 - 2018/03/19
91

  
92
    first release
93

  
94
=cut
95
"""
11 96

  
12 97
import os
13 98
import sys
14
import MySQLdb
15
import _mysql
99
import pkgutil
100

  
101

  
16 102
class ILIAS():
17
    title = "ILIAS session" 
18
    args = "--base 1000 -l 0"
19
    vlabel = "ilias"
20
    category = "ilias"
103
    pluginname = sys.argv[0].split('_')[1]
21 104

  
22 105
    def __init__(self):
23 106
        self.con = None
24 107
        self.user = os.environ.get('ildbuser', 'root')
25 108
        self.pw = os.environ.get('ildbpassword', '')
26
        self.ildb = os.environ.get('ildb','ilias')
109
        self.ildb = os.environ.get('ildb', 'ilias')
110
        self.ildbhost = os.environ.get('ildbhost', 'localhost')
111
        self.ildbport = int(os.environ.get('ildbport', 3306))
27 112

  
28
    def connectdb(self):
29
        self.con = MySQLdb.connect("localhost", self.user, self.pw,self.ildb)
30
        self.pluginname = sys.argv[0].split('_')[1]
113
    def db_modules_available(self):
114
        return pkgutil.find_loader("MySQLdb")
31 115

  
116
    def get_connection(self):
117
        import MySQLdb
118
        return MySQLdb.connect(host=self.ildbhost,
119
                               port=self.ildbport,
120
                               user=self.user,
121
                               passwd=self.pw,
122
                               db=self.ildb)
32 123

  
33
    def config_sessions(self):        
124
    def connectdb(self):
125
        self.con = self.get_connection()
126

  
127
    def config_sessions(self):
34 128
        print("graph_title ILIAS Session")
129
        print("graph_info Number of active ILIAS user sessions")
35 130
        print("graph_vlabel ilsessions")
36
        print("graph_category ILIAS")
131
        print("graph_category cms")
37 132
        print("ilsessions.label ilSessions")
133
        print("ilsessions.min 0")
134
        print("ilsessions.draw AREA")
38 135

  
39 136
    def execute_sessions(self):
40 137
        cursor = self.con.cursor()
41
        cursor.execute("select count(user_id) from usr_session where `expires` > UNIX_TIMESTAMP( NOW( ) ) AND user_id != 0")
138
        cursor.execute(
139
            "SELECT COUNT( user_id ) "
140
            "FROM usr_session "
141
            "WHERE `expires` > UNIX_TIMESTAMP( NOW( ) ) AND user_id != 0"
142
        )
42 143
        usrs = cursor.fetchone()[0]
43
        print("ilsessions %s"%(usrs))
44

  
45

  
46
    def config_10minavg(self):      
47
        print("graph_title ILIAS 10 avg")
48
        print("graph_vlabel il10minavg")
49
        print("graph_category ILIAS")
50
        print("il10minavg.label 10 min Count")
51

  
52
    def execute_10minavg(self):
144
        print("ilsessions.value %s" % (usrs))
145

  
146
    def config_5minavg(self):
147
        print("graph_title ILIAS 5 avg")
148
        print("graph_info ILIAS sessions created or "
149
              "updated during the last 5 minutes")
150
        print("graph_vlabel il5minavg")
151
        print("graph_category cms")
152
        print("il5minavg.label 5 min Count")
153
        print("il5minavg.min 0")
154
        print("il5minavg.draw AREA")
155

  
156
    def execute_5minavg(self):
53 157
        cursor = self.con.cursor()
54
        cursor.execute("select count(user_id) from usr_session where 10 * 60 > UNIX_TIMESTAMP( NOW( ) ) - ctime AND user_id != 0")
158
        cursor.execute(
159
            "SELECT COUNT( user_id ) "
160
            "FROM usr_session "
161
            "WHERE 5 * 60 > UNIX_TIMESTAMP( NOW( ) ) - ctime AND user_id != 0"
162
        )
55 163
        usrs = cursor.fetchone()[0]
56
        print("il10minavg %s"%(usrs))
164
        print("il5minavg.value %s" % (usrs))
57 165

  
58
    def config_60minavg(self):      
166
    def config_60minavg(self):
59 167
        print("graph_title ILIAS 60 avg")
168
        print("graph_info ILIAS sessions created or "
169
              "updated during the last 60 minutes")
60 170
        print("graph_vlabel il60minavg")
61
        print("graph_category ILIAS")
171
        print("graph_category cms")
62 172
        print("il60minavg.label 60 min Count")
173
        print("il60minavg.min 0")
174
        print("il60minavg.draw AREA")
63 175

  
64 176
    def execute_60minavg(self):
65 177
        cursor = self.con.cursor()
66
        cursor.execute("select count(user_id) from usr_session where 60 * 60 > UNIX_TIMESTAMP( NOW( ) ) - ctime AND user_id != 0")
178
        cursor.execute(
179
            "SELECT COUNT( user_id ) "
180
            "FROM usr_session "
181
            "WHERE 60 * 60 > UNIX_TIMESTAMP( NOW( ) ) - ctime AND user_id != 0"
182
        )
67 183
        usrs = cursor.fetchone()[0]
68
        print("il60minavg %s"%(usrs))
184
        print("il60minavg.value %s" % (usrs))
69 185

  
70
    def config_total1day(self):      
186
    def config_total1day(self):
71 187
        print("graph_title Users in 24h")
188
        print("graph_info ILIAS users logging in during last 24h")
72 189
        print("graph_vlabel iltotal1day")
73
        print("graph_category ILIAS")
190
        print("graph_category cms")
74 191
        print("iltotal1day.label User/24h")
192
        print("iltotal1day.min 0")
193
        print("iltotal1day.draw AREA")
75 194

  
76 195
    def execute_total1day(self):
77 196
        cursor = self.con.cursor()
78
        cursor.execute("select count(usr_id) FROM  `usr_data` WHERE last_login >= DATE_SUB( NOW( ) , INTERVAL 1 DAY )")
197
        cursor.execute(
198
            "SELECT COUNT( usr_id ) "
199
            "FROM `usr_data` "
200
            "WHERE last_login >= DATE_SUB( NOW( ) , INTERVAL 1 DAY )")
79 201
        usrs = cursor.fetchone()[0]
80
        print("iltotal1day %s"%(usrs))
81

  
202
        print("iltotal1day.value %s" % (usrs))
82 203

  
83 204
    def run(self):
84
        cmd =  ((len(sys.argv) > 1) and sys.argv[1] or None) or "execute"        
205
        cmd = ((len(sys.argv) > 1) and sys.argv[1]) or "execute"
85 206
        function = None
86
        if cmd == "execute":
87
            function = "execute"            
88
        elif cmd == "config":
207

  
208
        if cmd == "config":
89 209
            function = "config"
90
        if function != None:
91
            self.connectdb()
92
            try:
93
                func = getattr(self, "%s_%s"%(function,self.pluginname))
94
            except AttributeError:
95
                print 'function not found "%s" (%s)' % ("config_%s"%self.pluginname, "self")
210
        elif cmd == "suggest":
211
            print("sessions")
212
            print("5minavg")
213
            print("60minavg")
214
            print("total1day")
215
        elif cmd == "autoconf":
216
            if not self.db_modules_available():
217
                print("no (Please install the MySQLdb python3 module)")
218
            else:
219
                try:
220
                    con = self.get_connection()
221
                    cursor = con.cursor()
222
                    cursor.execute("SELECT COUNT( component ) "
223
                                   "FROM il_pluginslot")
224
                    con.close()
225
                except _mysql.Error as e:
226
                    print("no (Error %d: %s - Database configuration missing?)"
227
                          % (e.args[0], e.args[1]))
228
                else:
229
                    print("yes")
230
        else:
231
            function = "execute"
232

  
233
        if function is not None:
234
            if not self.db_modules_available():
235
                print("U (Please install the MySQLdb python3 module)")
96 236
            else:
97
                func()
237
                self.connectdb()
238
                try:
239
                    func = getattr(self, "%s_%s" % (function, self.pluginname))
240
                except AttributeError:
241
                    print('function not found "%s" (%s)' %
242
                          ("config_%s" % self.pluginname, "self"))
243
                else:
244
                    func()
245

  
98 246
        if self.con:
99 247
            self.con.close()
100
        if cmd == "suggest":
101
            print ("sessions")
102
            print ("10minavg")
103
            print ("60minavg")
104
            print ("total1day")
105
        if cmd == "autoconf":
106
            try:
107
                con = MySQLdb.connect("localhost", self.user, self.pw,self.ildb)
108
                cursor = con.cursor()
109
                cursor.execute("SELECT count(component) FROM  il_pluginslot")
110
            except _mysql.Error, e:              
111
                print "no (Error %d: %s)" % (e.args[0], e.args[1])
112
            else:    
113
                print "yes"
248

  
114 249
        sys.exit(0)
115 250

  
116 251

  
117 252
if __name__ == "__main__":
118 253
    ILIAS().run()
119
  
254

  

Formats disponibles : Unified diff