4 # rafael@postgresql.org.es / http://www.postgresql.org.es/
6 # Copyright (c) 2015 USIT-University of Oslo
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.
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.
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.
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/>.
30 # ###################################################################
31 # Method: get_local_filesystem_limits()
32 # Parameters: filesystem, limit_type
34 # Used to read the file /etc/zabbix_filesystems_limits.conf if it
35 # exists and the custom filesystems limits defined in this file.
37 # It will return -1 if no custom limit has been defined, or the filesystem
38 # is not defined in the file.
40 # Limit_type valid values: [percent_limit | size_limit]
42 # The format used to define custom limits for a filesystem is:
44 # <filesystem>::<percent_limit>::<size_limit>
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 '?'.
52 # Only mounted filesystem will be considered when reading filesystems
53 # limits definitions in /etc/zabbix_filesystems_limits.conf.
57 # Filesystem /mnt/my_filesystem limits will be 98% and 2048M:
58 # /mnt/my_filesystem::98::2048M
60 # Filesystems starting with /mnt limits will be 98% and 2048M:
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
67 # ###################################################################
70 def get_local_filesystem_limits(filesystem, limit_type):
72 defined_filesystems = dict()
73 config_file = '/etc/zabbix_filesystems_limits.conf'
77 if os.path.isfile(config_file):
78 fd = open(config_file, "r")
81 line = line.replace('\n', '').strip()
84 # Lines starting with # or empty lines are ignored.
87 if line.find('#', 0) == -1 and line.strip() != '':
88 filesystem_limit_definition = line.split('::')
91 # Only lines with the three parameters defined
95 if len(filesystem_limit_definition) == 3:
97 defined_fs = filesystem_limit_definition[0]
99 if filesystem_limit_definition[1] == '':
102 percent_limit = filesystem_limit_definition[1]
104 if filesystem_limit_definition[2] == '':
107 if filesystem_limit_definition[2][-1:].upper() == 'M':
108 size_limit = float(filesystem_limit_definition[2][:-1])*1024*1024
110 elif filesystem_limit_definition[2][-1:].upper() == 'G':
111 size_limit = float(filesystem_limit_definition[2][:-1])*1024*1024*1024
113 elif filesystem_limit_definition[2][-1:].upper() == 'T':
114 size_limit = float(filesystem_limit_definition[2][:-1])*1024*1024*1024*1024
119 # Normalized absolutized version of the pathname
120 # if filesystem does not include an absolute path
122 if not os.path.isabs(defined_fs):
123 defined_fs = os.path.abspath(defined_fs)
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 '?'.
135 expanded_paths = glob.glob(defined_fs)
138 # Only mounted filesystem will be considered when
139 # reading filesystems limits definitions in
140 # /etc/zabbix_filesystems_limits.conf.
143 for expanded_fs in expanded_paths:
144 if os.path.ismount(expanded_fs):
145 defined_filesystems[expanded_fs] = [percent_limit, size_limit]
148 # Find out if the filesystem we are asking for exists in
149 # /etc/zabbix_filesystems_limits.conf. If it does not exists,
153 if defined_filesystems.has_key(filesystem):
155 if limit_type == 'percent_limit':
156 print defined_filesystems[filesystem][0]
158 elif limit_type == 'size_limit':
159 print defined_filesystems[filesystem][1]
165 # Return -1 if the file /etc/filesystems_limits.conf does not
172 except Exception, exc:
176 # ############################################
178 # ############################################
180 if __name__ == '__main__':
184 if len(sys.argv) == 3:
186 limit_t = sys.argv[2]
188 get_local_filesystem_limits(fs, limit_t)
191 print "Error: Wrong number of parameters"
192 print 'Format: ' + sys.argv[0] + ' <filesystem> <percent_limit | size_limit>'