def register_dashboard(self, parms): device_controller = DeviceController(self.db, self.logger) parms["date_created"] = datetime.now() if len(parms["widget"]): for widget in parms["widget"]: #check if job already exist or not if widget["data_type"] == "cpu": job_id = self.db.jobs.find_one({"device_id": widget["device_id"], "data_type": "cpu"}, {"_id": 1}) elif widget["data_type"] == "memory": job_id = self.db.jobs.find_one({"device_id": widget["device_id"], "data_type": "memory"}, {"_id": 1}) elif widget["data_type"] == "interface_util": job_id = self.db.jobs.find_one({"device_id": widget["device_id"], "data_type": "interface_util", "int_name": widget["int_name"]}, {"_id": 1}) elif widget["data_type"] == "interface_pl": job_id = self.db.jobs.find_one({"device_id": widget["device_id"], "data_type": "interface_pl", "int_name": widget["int_name"]}, {"_id": 1}) if job_id: self.logger.info("Job already exist") widget["job_id"] = str(job_id["_id"]) else: widget["device"] = device_controller.get_device_snmp_parm(ObjectId(widget["device_id"])) if widget["device"]: #insert job to database insert_id = self.db.jobs.insert_one(widget) widget["job_id"] = str(insert_id.inserted_id) del [widget["_id"]] requests.post("http://localhost:4243/api/job/new", json=widget, verify=False) del widget["device"] self.logger.info("New Job, insert to database") else: self.logger.info("Device not found") self.db.dashboard.insert_one(parms)
def __init__(self, spi, pin, conn, pump_pin): self.pin = pin self.spi = spi self.conn = conn self.pump = DeviceController(pin=pump_pin) self.value = None self.status = None self.timestamp = None self.force = False
def __init__(self, pin, heater_pin, conn): self.pin = pin self.retries = 5 self.heater = DeviceController(pin=heater_pin) self.conn = conn self.sensor = Adafruit_DHT.DHT22 self.value = None self.status = None self.timestamp = None self.force = False
def get_location_by_hostname(self, hostname): hostname = hostname.split(".")[0] # print hostname device = self.db.list_devices.find_one({"hostname": hostname}) device_controller = DeviceController(self.db, self.logger) if device: # return device["location"] location_name = device_controller.get_location_name( device["location_id"]) return location_name else: return None
def search(self, parms): device_controller = DeviceController(self.db, self.logger) if "location" in parms.keys(): list_location_ip = device_controller.get_device_list_by_locationid( parms["location"]) if "device" in parms.keys(): list_device_ip = device_controller.get_device_list_by_hostname( parms["device"]) #Doing intersection between device search and device in location search_ip = list(set(list_location_ip) & set(list_device_ip)) print search_ip query_search = [] if search_ip: query_search.append(Query.terms('host', search_ip)) if parms["time"]: time_from = parms["time"]["from"].split(" ")[0] time_to = parms["time"]["to"].split(" ")[0] query_search.append( Query.range('@timestamp', gte=time_from, lte=time_to)) if parms["severityLevel"]: query_search.append( Query.terms('severity', parms["severityLevel"])) if parms["keywordMessage"]: message_search = [] message_search.append(parms["keywordMessage"]) query_search.append(Query.terms('message', message_search)) index = "syslog*" es = Elasticsearch(["http://192.168.100.249:9200"]) q = ElasticQuery(es=es, index=index, doc_type='doc') # q.query(Query.match_all()) q.size(1000) q.query(Query.bool(must=query_search)) #q.query(Aggregate.terms(must=query_search)) print q.json(indent=4) query_result = self.format_results(q.get()) return query_result #No index to query else: return []
def __init__(self): self.device_serial = self.get_device_serial() self.device_controller = DeviceController(devices_config) self.devices = self.device_controller.get_devices_info() topics = [self.device_serial + "/#"] print(self.devices) self.mqtt_client = MqttClient(mqtt_config, topics, self.onMessageReceivedCallback) self.mqtt_client.connect(self.device_serial) self.loop_thread = threading.Thread(target=self.mqtt_client.loop, daemon=True) self.publish_thread = threading.Thread( target=self.publish_device_data_periodically, daemon=True)
class TemperatureSensor: def __init__(self, pin, heater_pin, conn): self.pin = pin self.retries = 5 self.heater = DeviceController(pin=heater_pin) self.conn = conn self.sensor = Adafruit_DHT.DHT22 self.value = None self.status = None self.timestamp = None self.force = False def set_min_max(self, min_temperature, max_temperature): self.heater.add_condition( lambda temperature, c_type: min_temperature > temperature and c_type == 'temperature') def unset_min_max(self): self.heater.reset() def set_heater_state(self, state): self.heater.set_state(state) def get_heater_state(self): return self.heater.get_state() def read(self): _, self.value = Adafruit_DHT.read_retry(self.sensor, self.pin, self.retries) if self.value is not None: self.value = round(self.value, 1) if self.value > 80 or self.value < -40: self.status = 'INCOHERENT_READ' else: self.status = 'OK' if self.force is False: self.heater.verify_conditions(self.value, 'temperature') else: self.status = 'FAILED_TO_RETRIEVE' return self.value, self.status def save(self): self.timestamp = self.conn.save('TEMPERATURE', {'value': self.value, 'status': self.status}) def log(self): print(f"[TemperatureSensor] Temperature: {self.value} - Status: {self.status} - Timestamp: {self.timestamp}") def run(self, force): self.force = force self.read() self.save() self.log() return self.get_heater_state()
class PhotoSensor: def __init__(self, spi, pin, conn, lamp_pin): self.spi = spi self.pin = pin self.conn = conn self.lamp = DeviceController(pin=lamp_pin) self.value = None self.status = None self.timestamp = None self.force = False def set_min_max(self, min_illumination, max_illumination): self.lamp.add_condition(lambda photo, c_type: min_illumination > photo and c_type == 'photo') def unset_min_max(self): self.lamp.reset() def set_lamp_state(self, state): self.lamp.set_state(state) def get_lamp_state(self): return self.lamp.get_state() def read(self): # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7) r = self.spi.xfer2([1, 8 + self.pin << 4, 0]) self.value = ((r[1] & 3) << 8) + r[2] if self.value is not None: if self.value > 1023 or self.value < 0: self.status = 'VALUE_OUT_OF_BOUNDS' else: self.status = 'OK' self.value = round(50 * (cos(pi * self.value / 1023) + 1)) if self.force is False: self.lamp.verify_conditions(self.value, 'photo') else: self.status = 'CANNOT_READ_LDR' return self.value, self.status def save(self): self.timestamp = self.conn.save('ILLUMINATION', {'value': self.value, 'status': self.status}) def log(self): print(f"[PhotoSensor] Value: {self.value} - Status: {self.status} - Timestamp: {self.timestamp}") def run(self, force): self.force = force self.read() self.save() self.log() return self.get_lamp_state()
def register_link(self, parms): self.logger.info(parms) try: device_controller = DeviceController(self.db, self.logger) src_id = device_controller.get_device_id(parms["src_host"]) dst_id = device_controller.get_device_id(parms["dst_host"]) data = { "src_id": src_id, "src_if": parms["src_if"] if "src_if" in parms.keys() else "", "src_ip": parms["src_ip"] if "src_ip" in parms.keys() else "", "dst_id": dst_id, "dst_if": parms["dst_if"] if "dst_if" in parms.keys() else "", "dst_ip": parms["dst_ip"] if "dst_ip" in parms.keys() else "", } self.db.link_devices.insert(data) return True except: return False
def main(): # config.ini 설정변수 읽기 try: config = configparser.ConfigParser() config.read('config.ini') apk_directory = config.get('device_controller', 'apk_directory') apk_list = list_apk(apk_directory) except Exception as e: print(str(e) + ' config.ini파일 읽는중에 에러발생') raise e # DeviceController객체 생성 controller = DeviceController() # 디렉토리 안에 들어있는 모든 APK파일에 대해서 진행 for apk in apk_list: controller.run_test(apk)
def get_registered_link(self): registered_link = [] device_controller = DeviceController(self.db, self.logger) list_registered_link = list(self.db.link_devices.find()) for link in list_registered_link: src_host = device_controller.get_device_by_id( link["src_id"])["hostname"] dst_host = device_controller.get_device_by_id( link["dst_id"])["hostname"] data = {} data["src_host"] = src_host data["dst_host"] = dst_host data["src_ip"] = link["src_ip"] data["dst_ip"] = link["dst_ip"] data["src_if"] = link["src_if"] data["dst_if"] = link["dst_if"] data["id_link"] = link["_id"] registered_link.append(data) return registered_link
def get_latency_jitter(self, id): link_registered = self.db.link_devices.find_one({"_id": ObjectId(id)}) device_controller = DeviceController(self.db, self.logger) src_host = device_controller.get_device_by_id( link_registered["src_id"])["hostname"] dst_host = device_controller.get_device_by_id( link_registered["dst_id"])["hostname"] latency_jitter = list( self.db.jitter.aggregate([{ "$match": { "id_link": ObjectId(id) } }, { "$sort": { "timestamp": -1 } }, { "$limit": 10 }, { "$project": { "latency": 1, "jitter": 1, "timestamp": 1, "_id": 0 } }])) for data in latency_jitter: data["timestamp"] = "{:d}:{:02d}".format(data["timestamp"].hour, data["timestamp"].minute) link_registered["src_host"] = src_host link_registered["dst_host"] = dst_host del link_registered["src_id"] del link_registered["dst_id"] del link_registered["_id"] latency_jitter.reverse() link_registered["latency_jitter"] = latency_jitter return link_registered
def format_results(self, results): device_controller = DeviceController(self.db, self.logger) device_map = device_controller.get_device_map_ip_hostname() location_map = device_controller.get_location_map_ip_location() """Print results nicely: doc_id) content""" query_result = [] data = [doc for doc in results['hits']['hits']] # print data for doc in data: # self.logger.info(doc["_source"]) data = {} data["severity_label"] = doc["_source"]["severity_label"] data["severity"] = doc["_source"]["severity"] data["timestamp"] = doc["_source"]["@timestamp"] data["host"] = doc["_source"]["host"] data["type"] = doc["_source"]["type"] data["message"] = doc["_source"]["message"] data["hostname"] = device_map[doc["_source"]["host"]] data["location"] = location_map[doc["_source"]["host"]] query_result.append(data) return query_result
def is_border_device(self, device): try: neighbor_data = self.get_latest_single_neighbor_data( device["hostname"]) location_set = set() device_controller = DeviceController(self.db, self.logger) location_name = device_controller.get_location_name( device["location_id"]) if (neighbor_data): neighbors = neighbor_data["neighbors"] # location_set.add(device["location"]) location_set.add(location_name) for neighbor in neighbors: location = self.get_location_by_hostname( neighbor["device_id"]) if location: location_set.add(location) if len(location_set) > 1: return {"result": 1, "neighbor_data": neighbor_data} else: return {"result": 0, "neighbor_data": neighbor_data} except pymongo.errors as e: print(e.message)
def save_baseline(self, parms): device_controller = DeviceController(self.db, self.logger) # parms["location_id"] = device_controller.get_location_id(parms["location"]) parms["timestamp"] = datetime.now() # del parms["location"] self.db.topology_baseline.insert_one(parms)
class SmartHomeController: device_controller = None mqtt_client = None device_serial = None devices = None loop_thread = None publish_thread = None def __init__(self): self.device_serial = self.get_device_serial() self.device_controller = DeviceController(devices_config) self.devices = self.device_controller.get_devices_info() topics = [self.device_serial + "/#"] print(self.devices) self.mqtt_client = MqttClient(mqtt_config, topics, self.onMessageReceivedCallback) self.mqtt_client.connect(self.device_serial) self.loop_thread = threading.Thread(target=self.mqtt_client.loop, daemon=True) self.publish_thread = threading.Thread( target=self.publish_device_data_periodically, daemon=True) def onMessageReceivedCallback(self, mqttc, user_data, msg): if (msg.topic == (self.device_serial + "/devices/info/req")): self.mqtt_client.publish( self.device_serial + "/devices/info", json.dumps(self.device_controller.get_devices_info())) return device = msg.topic.split("/")[1] devices_names = list(self.devices.keys()) if (device in devices_names): # Set device state message = msg.payload.decode("utf-8") active = None if (message == "On"): active = True if (message == "Off"): active = False self.device_controller.set_device_active(device, active) self.mqtt_client.publish( self.device_serial + "/devices/" + device + "/status", json.dumps(self.device_controller.get_devices_info()[device])) def publish_device_data_periodically(self): # Periodically send devices info while True: self.mqtt_client.publish( self.device_serial + "/devices/info", json.dumps(self.device_controller.get_devices_info())) time.sleep(10) def run(self): # Start threads and wacth their status while True: loop_thread_alive = sh.loop_thread.isAlive() publish_thread_alive = sh.publish_thread.isAlive() print("loop thread active: " + str(loop_thread_alive)) print("publish thread active: " + str(publish_thread_alive)) if not loop_thread_alive: sh.loop_thread.start() if not publish_thread_alive: sh.publish_thread.start() time.sleep(1) @staticmethod def get_device_serial(): device_serial = "0000000000000000" with open('/proc/cpuinfo', 'r') as f: device_serial = f.read() search = re.search(r"\nSerial\s+:\s+(?P<serial>[0-9a-f]{16})", device_serial) if search is None: raise BaseException("Cannot find device serial!") return search.group("serial")
class HumiditySensor: def __init__(self, spi, pin, conn, pump_pin): self.pin = pin self.spi = spi self.conn = conn self.pump = DeviceController(pin=pump_pin) self.value = None self.status = None self.timestamp = None self.force = False def set_min_max(self, min_humidity, max_humidity): self.pump.add_condition(lambda humidity, c_type: min_humidity > humidity and c_type == 'humidity') def unset_min_max(self): self.pump.reset() def set_pump_state(self, state): self.pump.set_state(state) def get_pump_state(self): return self.pump.get_state() def read(self): # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7) r = self.spi.xfer2([1, 8 + self.pin << 4, 0]) self.value = ((r[1] & 3) << 8) + r[2] if self.value is not None: if self.value > 1023 or self.value < 0: self.status = 'INCOHERENT_READ' else: self.value = round(50 * (cos(pi * self.value / 1023) + 1)) self.status = 'OK' if self.force is False: self.pump.set_duration(5) self.pump.verify_conditions(self.value, 'humidity') else: self.pump.set_duration(None) else: self.status = 'FAILED_TO_RETRIEVE' return self.value, self.status def save(self): self.timestamp = self.conn.save('HUMIDITY', { 'value': self.value, 'status': self.status }) def log(self): print( f"[HumiditySensor] Humidity: {self.value} - Status: {self.status} - Timestamp: {self.timestamp}" ) def run(self, force): self.force = force self.read() self.save() self.log() return self.get_pump_state()