Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / dvb / femon @ 17f78427

Historique | Voir | Annoter | Télécharger (7,72 ko)

1
#!/usr/bin/env python
2
# -*- encoding: iso-8859-1 -*-
3
#
4
# Wildcard-plugin to monitor DVB signal information via femon command line utility,
5
#
6
# To monitor a dvb device, link femon_<device> to this file.
7
# E.g.
8
#    ln -s /usr/share/munin/plugins/femon_ /etc/munin/plugins/femon_adapter0
9
# ...will monitor /dev/dvb/adapter0.
10
#
11
# Needs following minimal configuration in plugin-conf.d/munin-node:
12
#   [femon_*]
13
#   user root
14
#
15
# Parameters
16
# 	femonpath     - Specify path to femon program (Default: /usr/bin/femon)
17
#       graph_args    - Specify graph args (Default: --lower-limit 0 --upper-limit 100 --rigid)
18
#
19
# Author: Nicolas Knotzer  <nknotzer@gmail.com>
20
#
21
# v1.0 02/10/2011
22
# v1.1 20/10/2011 - Prints OSError.strerror in verbose mode, uses rsplit instead of split for parsing femon output
23
# v1.2 21/10/2011 - Uses subprocess.Popen instead of subprocess.check_output for better compatibility with older python versions (i.e. works with python version >= 2.4)
24
# v1.3 25/10/2011 - Configure upper and lower graph limits with graph_args environment variable.
25
#
26
# Copyright (c) 2011 Nicolas Knotzer.
27
#
28
# The smart_ plugin by Nicolas Stransky <Nico@stransky.cx> was used as a template for this plugin!
29
#
30
# Permission to use, copy, and modify this software with or without fee
31
# is hereby granted, provided that this entire notice is included in
32
# all source code copies of any software which is or includes a copy or
33
# modification of this software.
34
#
35
# THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
36
# IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
37
# REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
38
# MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
39
# PURPOSE.
40
#
41
#
42
# Magic markers
43
#%# capabilities=autoconf suggest
44
#%# family=auto
45

    
46
## You may edit the following 2 variables
47
# Increase verbosity (True/False)
48
verbose=False
49

    
50
# You may not modify anything below this line
51

    
52
import os, sys, string, subprocess
53
from math import log
54
plugin_version="1.3"
55

    
56
def verboselog(s):
57
    global plugin_name
58
    sys.stderr.write(plugin_name+': '+s+'\n')
59

    
60
if not verbose :
61
    verboselog = lambda s: None
62

    
63
def find_dvb_adapters() :
64
    adapters=[]
65
    if os.path.exists('/dev/dvb/'):
66
        try :
67
            for adapter in os.listdir('/dev/dvb/') :
68
                try :
69
                    verboselog('Found '+adapter+'...')
70
                    adapters.append(adapter)
71
                except :
72
                    continue
73
        except :
74
            verboselog('Failed to list adapters in /dev/dvb')
75
    return(adapters)
76

    
77
def get_dvb_adapter_name() :
78
    global plugin_name
79
    try :
80
        name=[plugin_name[string.rindex(plugin_name,'_')+1:]]
81

    
82
        # Check that the adapter exists in /dev/dvb
83
        if not os.path.exists('/dev/dvb/'+name[0]):
84
            verboselog('/dev/dvb/'+name[0]+' not found!')
85
            sys.exit(1)
86
        return(name)
87
    except :
88
        verboselog('No dvb adapter name found in plugin\'s symlink!')
89
        sys.exit(1)
90

    
91
def print_adapter_config(dvb_adapter) :
92
    verboselog('Printing configuration')
93
    print ('graph_title DVB Femon Sensors '+dvb_adapter[0])
94
    print ('graph_args '+os.getenv('graph_args','--lower-limit 0 --upper-limit 100 --rigid'))
95
    print ('graph_vlabel Quality')
96
    print ('graph_category tv')
97
    print ('graph_info This graph shows femon output for your dvb-'+dvb_adapter[0])
98

    
99
    print ('str.label Signal Strength')
100
    print ('str.info Signal Strength')
101
    print ('str.draw LINE2')
102

    
103
    print ('snr.label Signal-to-Noise Ratio')
104
    print ('snr.info Signal-to-Noise Ratio')
105
    print ('snr.draw LINE2')
106

    
107
    print ('ber.label Bit Error Rate')
108
    print ('ber.info Bit Error Rate')
109
    print ('ber.draw LINE2')
110

    
111
    print ('unc.label Uncorrected Blocks')
112
    print ('unc.info Uncorrected Blocks')
113
    print ('unc.draw LINE2')
114

    
115

    
116
def print_dvb_adapter_values(dvb_adapter) :
117
    try :
118
        verboselog('Reading values from '+dvb_adapter[0])
119
        mypipe = subprocess.Popen([os.getenv('femonpath','/usr/bin/femon'), '-H', '-c 1', '-a '+dvb_adapter[0].replace('adapter','')], stdout=subprocess.PIPE)
120
        femon_output = mypipe.communicate()[0]
121
	verboselog(femon_output)
122
    except OSError, e:
123
        verboselog('Cannot access femon values! Check user rights or proper femon installation.')
124
        verboselog('Error: '+e.strerror)
125
        sys.exit(1)
126
    try :
127
        splitstr=femon_output.rsplit ('|',5)
128
        print ('str.value '+splitstr[1].replace('signal','').strip(' %'))
129
        print ('snr.value '+splitstr[2].replace('snr','').strip(' %'))
130
        print ('ber.value '+splitstr[3].replace('ber','').strip(' '))
131
        print ('unc.value '+splitstr[4].replace('unc','').strip(' '))
132
    except :
133
        verboselog ('Error processing femon output string.')
134
        sys.exit(1)
135

    
136
### Main part ###
137

    
138
plugin_name=list(os.path.split(sys.argv[0]))[1]
139
verboselog('plugins\' UID: '+str(os.geteuid())+' / plugins\' GID: '+str(os.getegid()))
140

    
141
# Parse arguments
142
if len(sys.argv)>1 :
143
    if sys.argv[1]=="config" :
144
        dvb_adapter=get_dvb_adapter_name()
145
        print_adapter_config (dvb_adapter)
146
        sys.exit(0)
147
    elif sys.argv[1]=="autoconf" :
148
        if os.path.exists(os.getenv('femonpath','/usr/bin/femon')) :
149
            if not find_dvb_adapters():
150
                print('no (no dvb adapters accessible)')
151
            else :
152
                print('yes')
153
            sys.exit(0)
154
        else :
155
            print('no (femon not found)')
156
            sys.exit(0)
157
    elif sys.argv[1]=="suggest" :
158
        for adapter in find_dvb_adapters() :
159
            print(adapter)
160
        sys.exit(0)
161
    elif sys.argv[1]=="version" :
162
        print('femon_ Munin plugin, version '+plugin_version)
163
        sys.exit(0)
164
    elif sys.argv[1]!="" :
165
        verboselog('unknown argument "'+sys.argv[1]+'"')
166
        sys.exit(1)
167

    
168
# No argument given, doing the real job:
169
dvb_adapter=get_dvb_adapter_name()
170
print_dvb_adapter_values (dvb_adapter)
171
exit(0)
172

    
173

    
174
### The following is the _ plugin documentation, intended to be used with munindoc
175
"""
176
=head1 NAME
177

    
178
femon_ - Munin wildcard-plugin to monitor dvb signal information attribute values through femon
179

    
180
=head1 APPLICABLE SYSTEMS
181

    
182
Node with B<Python> interpreter and B<femon>
183
installed and in function.
184

    
185
=head1 CONFIGURATION
186

    
187
=head2 Create link in service directory
188

    
189
To monitor a dvb device, create a link in the service directory
190
of the munin-node named femon_<device>, which is pointing to this file.
191

    
192
E.g.
193

    
194
ln -s /usr/share/munin/plugins/femon_ /etc/munin/plugins/femon_adapter0
195

    
196
...will monitor /dev/dvb/adapter0.
197

    
198
=head2 Grant privileges in munin-node
199

    
200
The plugin must be run under high privileged user B<root>, to get access to the raw device.
201

    
202
So following minimal configuration in plugin-conf.d/munin-node is needed.
203

    
204
=over 2
205

    
206
  [femon_*]
207
  user root
208

    
209
=back
210

    
211
=head2 Set Parameter if needed
212

    
213
  femonpath     - Specify path to femon program (Default: /usr/bin/femon)
214

    
215
=head1 INTERPRETATION
216

    
217

    
218
=head1 MAGIC MARKERS
219

    
220
 #%# family=auto
221
 #%# capabilities=autoconf suggest
222

    
223
=head1 CALL OPTIONS
224

    
225
B<none>
226

    
227
=over 2
228

    
229
Fetches values if called without arguments:
230

    
231
E.g.: munin-run femon_adapter0
232

    
233
=back
234

    
235
B<config>
236

    
237
=over 2
238

    
239
Prints plugins configuration.
240

    
241
E.g.: munin-run femon_adapter0 config
242

    
243
=back
244

    
245
B<autoconf>
246

    
247
=over 2
248

    
249
Tries to find femon and outputs value 'yes' for success, 'no' if not.
250

    
251
It's used by B<munin-node-configure> to see whether autoconfiguration is possible.
252

    
253
=back
254

    
255
B<suggest>
256

    
257
=over 2
258

    
259
Outputs the list of device names, that it found plugged to the system.
260

    
261
B<munin-node-configure> use this to build the service links for this wildcard-plugin.
262

    
263
=back
264

    
265
=head1 VERSION
266

    
267
Version 1.3
268

    
269
=head1 BUGS
270

    
271
None known
272

    
273
=head1 AUTHOR
274

    
275
(C) 2011 Nicolas Knotzer <nknotzer@gmail.com>
276

    
277
The smart_ plugin by Nicolas Stransky <Nico@stransky.cx> was used as a template for this plugin!
278

    
279
=head1 LICENSE
280

    
281
GPLv2 (http://www.gnu.org/licenses/gpl-2.0.txt)
282

    
283
=cut
284

    
285

    
286
"""