1+ #!/usr/bin/env python
2+ #Copyright (c) 2014, Paessler AG <[email protected] > 3+ #All rights reserved.
4+ #Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
5+ # following conditions are met:
6+ #1. Redistributions of source code must retain the above copyright notice, this list of conditions
7+ # and the following disclaimer.
8+ #2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
9+ # and the following disclaimer in the documentation and/or other materials provided with the distribution.
10+ #3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse
11+ # or promote products derived from this software without specific prior written permission.
12+
13+ #THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
14+ # INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15+ # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
16+ # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17+ # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18+ # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
19+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
20+ # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
21+
22+
23+ ### PRTG Python Miniprobe
24+ ### Miniprobe needs at least Python 2.7 because of "importlib"
25+ ### If older python version is used you will have to install "importlib"
26+
27+ # import general modules
28+ import sys
29+ import hashlib
30+ import importlib
31+ import gc
32+ import logging
33+
34+
35+ # import own modules
36+ sys .path .append ('./' )
37+
38+ try :
39+ import sensors
40+ import requests
41+ except Exception as e :
42+ print e
43+ #sys.exit()
44+
45+
46+ class MiniProbe (object ):
47+ """
48+ Main class for the Python Mini Probe
49+ """
50+ def __init__ (self ):
51+ gc .enable ()
52+ logging .basicConfig (
53+ filename = "./logs/probe.log" ,
54+ filemode = "a" ,
55+ level = logging .DEBUG ,
56+ format = "%(asctime)s - %(levelname)s - %(message)s" ,
57+ datefmt = '%m/%d/%Y %H:%M:%S'
58+ )
59+
60+ def get_import_sensors (self ):
61+ """
62+ import available sensor modules and return list of sensor objects
63+ """
64+ sensor_objects = []
65+ for mod in sensors .__all__ :
66+ try :
67+ sensor_objects .append (self .load_class ("sensors.%s.%s" % (mod .lower (), mod )))
68+ except Exception as import_error :
69+ print import_error
70+ return sensor_objects
71+
72+ @staticmethod
73+ def load_class (full_class_string ):
74+ """
75+ dynamically load a class from a string
76+ """
77+ class_data = full_class_string .split ("." )
78+ module_path = "." .join (class_data [:- 1 ])
79+ class_str = class_data [- 1 ]
80+ module = importlib .import_module (module_path )
81+ return getattr (module , class_str )
82+
83+ def read_config (self , path ):
84+ """
85+ read configuration file and write data to dict
86+ """
87+ config = {}
88+ try :
89+ conf_file = open (path )
90+ for line in conf_file :
91+ if not (line == '\n ' ):
92+ if not (line .startswith ('#' )):
93+ config [line .split (':' )[0 ]] = line .split (':' )[1 ].rstrip ()
94+ conf_file .close ()
95+ return config
96+ except Exception as read_error :
97+ logging .error ("No config found! Error Message: %s Exiting!" % read_error )
98+ sys .exit ()
99+
100+ @staticmethod
101+ def hash_access_key (key ):
102+ """
103+ create hash of probes access key
104+ """
105+ return hashlib .sha1 (key ).hexdigest ()
106+
107+ def create_parameters (self , config , jsondata , i = None ):
108+ """
109+ create URL parameters for announce, task and data requests
110+ """
111+ if i == 'announce' :
112+ return {'gid' : config ['gid' ], 'key' : self .hash_access_key (config ['key' ]), 'protocol' : config ['protocol' ],
113+ 'name' : config ['name' ], 'baseinterval' : config ['baseinterval' ], 'sensors' : jsondata }
114+ else :
115+ return {'gid' : config ['gid' ], 'key' : self .hash_access_key (config ['key' ]), 'protocol' : config ['protocol' ]}
116+
117+ def create_url (self , config , i = None ):
118+ """
119+ creating the actual URL
120+ """
121+ if not (i is None ) and (i != "data" ):
122+ return "https://%s:%s/probe/%s" % (
123+ config ['server' ], config ['port' ], i )
124+ elif i == "data" :
125+ return "https://%s:%s/probe/%s?gid=%s&protocol=%s&key=%s" % (config ['server' ], config ['port' ], i ,
126+ config ['gid' ], config ['protocol' ],
127+ self .hash_access_key (config ['key' ]))
128+ pass
129+ else :
130+ return "No method given"
131+
132+ def build_announce (self , sensor_list ):
133+ """
134+ build json for announce request
135+ """
136+ sensors_avail = []
137+ for sensor in sensor_list :
138+ sensors_avail .append (sensor .get_sensordef ())
139+ return sensors_avail
0 commit comments