]> git.uio.no Git - uio-zabbix.git/blob - zabbix_filesystem_limits.py
Add zabbix autodiscovery script for rabbitmq
[uio-zabbix.git] / zabbix_filesystem_limits.py
1 #!/usr/bin/env python
2 #
3 # Authors:
4 # rafael@postgresql.org.es / http://www.postgresql.org.es/
5 #
6 # Copyright (c) 2015 USIT-University of Oslo
7 #
8 # zabbix_filesystem_limits.py: Used by zabbix_agent to find out if
9 # custom filesystem limits have been defined for some of the mounted
10 # filesystem in a server.
11 #
12 # zabbix_filesystem_limits.py is free software: you can redistribute
13 # it and/or modify it under the terms of the GNU General Public
14 # License as published by the Free Software Foundation, either version
15 # 3 of the License, or (at your option) any later version.
16 #
17 # zabbix_filesystem_limits.py is distributed in the hope that it will
18 # be useful, but WITHOUT ANY WARRANTY; without even the implied
19 # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 # See the GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with sms_send.  If not, see <http://www.gnu.org/licenses/>.
24 #
25
26 import os
27 import sys
28 import glob
29
30 # ###################################################################
31 # Method: get_local_filesystem_limits()
32 # Parameters: filesystem, limit_type
33 #
34 # Used to read the file /etc/zabbix_filesystems_limits.conf if it
35 # exists and the custom filesystems limits defined in this file.
36 #
37 # It will return -1 if no custom limit has been defined, or the filesystem
38 # is not defined in the file.
39 #
40 # Limit_type valid values: [percent_limit | size_limit]
41 #
42 # The format used to define custom limits for a filesystem is:
43 #
44 # <filesystem>::<percent_limit>::<size_limit>
45 #
46 # Filesystems can match a specified pattern according to the rules
47 # used by the Unix shell. No tilde expansion is done, but *, ?, and
48 # character ranges expressed with [] will be correctly matched. For a
49 # literal match, wrap the meta-characters in brackets. For example,
50 # '[?]' matches the character '?'.
51 #
52 # Only mounted filesystem will be considered when reading filesystems
53 # limits definitions in /etc/zabbix_filesystems_limits.conf.
54 #
55 # Examples:
56 #
57 # Filesystem /mnt/my_filesystem limits will be 98% and 2048M:
58 # /mnt/my_filesystem::98::2048M
59 #
60 # Filesystems starting with /mnt limits will be 98% and 2048M:
61 # /mnt/*::98::2048M
62 #
63 # Filesystems starting with /mnt and a or b in the path third level
64 # limits will be 98% and 2048M: 
65 # /mnt/*/[ab]*::98::2048M
66 #
67 # ###################################################################
68
69
70 def get_local_filesystem_limits(filesystem, limit_type):
71
72     defined_filesystems = dict()
73     config_file = '/etc/zabbix_filesystems_limits.conf'
74
75     try:
76
77         if os.path.isfile(config_file):
78             fd = open(config_file, "r")
79
80             for line in fd:
81                 line = line.replace('\n', '').strip()
82
83                 #
84                 # Lines starting with # or empty lines are ignored.
85                 #
86
87                 if line.find('#', 0) == -1 and line.strip() != '':
88                     filesystem_limit_definition = line.split('::')
89
90                     #
91                     # Only lines with the three parameters defined
92                     # will be considered.
93                     #
94
95                     if len(filesystem_limit_definition) == 3:
96
97                         defined_fs = filesystem_limit_definition[0]
98
99                         if filesystem_limit_definition[1] == '':
100                             percent_limit = "-1"
101                         else:
102                             percent_limit = filesystem_limit_definition[1]
103
104                         if filesystem_limit_definition[2] == '':
105                             size_limit = "-1"
106                         else:
107                             if filesystem_limit_definition[2][-1:].upper() == 'M':
108                                 size_limit = float(filesystem_limit_definition[2][:-1])*1024*1024
109
110                             elif filesystem_limit_definition[2][-1:].upper() == 'G':
111                                 size_limit = float(filesystem_limit_definition[2][:-1])*1024*1024*1024
112
113                             elif filesystem_limit_definition[2][-1:].upper() == 'T':
114                                 size_limit = float(filesystem_limit_definition[2][:-1])*1024*1024*1024*1024
115
116                             else:
117                                 size_limit = "-1"
118
119                         # Normalized absolutized version of the pathname
120                         # if filesystem does not include an absolute path
121
122                         if not os.path.isabs(defined_fs):
123                             defined_fs = os.path.abspath(defined_fs)
124
125                         #
126                         # Finds all the pathnames matching a specified
127                         # pattern according to the rules used by the Unix
128                         # shell. No tilde expansion is done, but *, ?, and
129                         # character ranges expressed with [] will be
130                         # correctly matched. For a literal match, wrap the
131                         # meta-characters in brackets. For example, '[?]'
132                         # matches the character '?'.
133                         #
134
135                         expanded_paths = glob.glob(defined_fs)
136
137                         #
138                         # Only mounted filesystem will be considered when
139                         # reading filesystems limits definitions in
140                         # /etc/zabbix_filesystems_limits.conf.
141                         #
142
143                         for expanded_fs in expanded_paths:
144                             if os.path.ismount(expanded_fs):
145                                 defined_filesystems[expanded_fs] = [percent_limit, size_limit]
146
147             #
148             # Find out if the filesystem we are asking for exists in
149             # /etc/zabbix_filesystems_limits.conf. If it does not exists,
150             # return -1
151             #
152
153             if defined_filesystems.has_key(filesystem):
154
155                 if limit_type == 'percent_limit':
156                     print defined_filesystems[filesystem][0]
157
158                 elif limit_type == 'size_limit':
159                     print defined_filesystems[filesystem][1]
160
161             else:
162                 print "-1"
163
164         #
165         # Return -1 if the file /etc/filesystems_limits.conf does not
166         # exist.
167         #
168
169         else:
170             print "-1"
171
172     except Exception, exc:
173         raise exc
174
175
176 # ############################################
177 # Main
178 # ############################################
179
180 if __name__ == '__main__':
181
182     try:
183
184         if len(sys.argv) == 3:
185             fs = sys.argv[1]
186             limit_t = sys.argv[2]
187
188             get_local_filesystem_limits(fs, limit_t)
189
190         else:
191             print "Error: Wrong number of parameters"
192             print 'Format: ' + sys.argv[0] + ' <filesystem> <percent_limit | size_limit>'
193             sys.exit(1)
194
195     except Exception, e:
196         print e
197         sys.exit(1)