]>
Commit | Line | Data |
---|---|---|
ad51e17e | 1 | #!/usr/bin/python2 |
e0ddb98a RMG |
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_not_monitored.py: Used by zabbix_agent to find out if | |
9 | # a filesystem does not have to be monitored. | |
10 | # | |
11 | # zabbix_filesystem_not_monitored.py is free software: you can redistribute | |
12 | # it and/or modify it under the terms of the GNU General Public | |
13 | # License as published by the Free Software Foundation, either version | |
14 | # 3 of the License, or (at your option) any later version. | |
15 | # | |
16 | # zabbix_filesystem_not_monitored.py is distributed in the hope that it will | |
17 | # be useful, but WITHOUT ANY WARRANTY; without even the implied | |
18 | # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
19 | # See the GNU General Public License for more details. | |
20 | # | |
21 | # You should have received a copy of the GNU General Public License | |
22 | # along with sms_send. If not, see <http://www.gnu.org/licenses/>. | |
23 | # | |
24 | ||
25 | import os | |
26 | import sys | |
27 | import glob | |
28 | ||
29 | # ################################################################### | |
30 | # Method: get_filesystem_not_monitored_status() | |
31 | # Parameters: filesystem | |
32 | # | |
33 | # Used to read the file /etc/zabbix_filesystems_not_monitored.conf if it | |
34 | # exists. | |
35 | # | |
36 | # It will return 0 if filesystem is not defined in this file or if the | |
37 | # file does not exist, 1 otherwise. | |
38 | # | |
39 | # The format used to define a filesystem that will not be monitored is a | |
40 | # filesystem definition per line: | |
41 | # | |
42 | # <filesystem> | |
43 | # | |
44 | # Filesystems can match a specified pattern according to the rules | |
45 | # used by the Unix shell. No tilde expansion is done, but *, ?, and | |
46 | # character ranges expressed with [] will be correctly matched. For a | |
47 | # literal match, wrap the meta-characters in brackets. For example, | |
48 | # '[?]' matches the character '?'. | |
49 | # | |
50 | # Only mounted filesystem will be considered when reading filesystems | |
51 | # limits definitions in /etc/zabbix_filesystems_not_monitored.conf. | |
52 | # | |
53 | # ################################################################### | |
54 | ||
55 | ||
56 | def get_filesystem_not_monitored_status(filesystem): | |
57 | ||
58 | defined_filesystems = [] | |
59 | config_file = '/etc/zabbix_filesystems_not_monitored.conf' | |
60 | ||
61 | try: | |
62 | ||
63 | if os.path.isfile(config_file): | |
64 | fd = open(config_file, "r") | |
65 | ||
66 | for line in fd: | |
67 | line = line.replace('\n', '').strip() | |
68 | ||
69 | # | |
70 | # Lines starting with # or empty lines are ignored. | |
71 | # | |
72 | ||
73 | if line.find('#', 0) == -1 and line.strip() != '': | |
74 | ||
75 | defined_fs = line | |
76 | ||
77 | # Normalized absolutized version of the pathname | |
78 | # if filesystem does not include an absolute path | |
79 | ||
80 | if not os.path.isabs(defined_fs): | |
81 | defined_fs = os.path.abspath(defined_fs) | |
82 | ||
83 | # | |
84 | # Finds all the pathnames matching a specified | |
85 | # pattern according to the rules used by the Unix | |
86 | # shell. No tilde expansion is done, but *, ?, and | |
87 | # character ranges expressed with [] will be | |
88 | # correctly matched. For a literal match, wrap the | |
89 | # meta-characters in brackets. For example, '[?]' | |
90 | # matches the character '?'. | |
91 | # | |
92 | ||
93 | expanded_paths = glob.glob(defined_fs) | |
94 | ||
95 | # | |
96 | # Only mounted filesystem will be considered when | |
97 | # reading filesystems definitions in | |
98 | # /etc/zabbix_filesystems_not_monitored.conf. | |
99 | # | |
100 | ||
101 | for expanded_fs in expanded_paths: | |
102 | if os.path.ismount(expanded_fs): | |
103 | defined_filesystems.append(expanded_fs) | |
104 | ||
e0ddb98a RMG |
105 | # |
106 | # Find out if the filesystem we are asking for exists in | |
107 | # /etc/zabbix_filesystems_not_monitored.conf. If it does | |
108 | # not exists, return 0 | |
109 | # | |
110 | ||
111 | if filesystem in defined_filesystems: | |
112 | print "1" | |
113 | else: | |
114 | print "0" | |
115 | ||
116 | else: | |
117 | # | |
118 | # Return 0 if the file /etc/filesystems_not_monitored.conf | |
119 | # does not exist. | |
120 | # | |
121 | ||
122 | print "0" | |
123 | ||
124 | except Exception, exp: | |
125 | raise exp | |
126 | ||
127 | ||
128 | # ############################################ | |
129 | # Main | |
130 | # ############################################ | |
131 | ||
132 | if __name__ == '__main__': | |
133 | ||
134 | try: | |
135 | ||
136 | if len(sys.argv) == 2: | |
137 | fs = sys.argv[1] | |
138 | ||
139 | get_filesystem_not_monitored_status(fs) | |
140 | ||
141 | else: | |
142 | print "Error: Wrong number of parameters" | |
143 | print 'Format: ' + sys.argv[0] + ' <filesystem>' | |
144 | sys.exit(1) | |
145 | ||
146 | except Exception, e: | |
147 | print e | |
148 | sys.exit(1) |