def runner(): known_wifis = set() for _, step_time in timed_loop(5, name="wifi_scan"): wifis = check_output("/usr/sbin/wpa_cli scan_results".split(" ")) wifis = [l.split("\t") for l in wifis.decode().strip().split("\n")[2:]] wifis = [(bssid, ssid) for bssid, freq, signal, flags, ssid in wifis] wifis = set(wifis) for bssid, ssid in known_wifis - wifis: # lost wifi push_message( "wifi_analyzer", { "event": "lost_wifi", "timestamp": d2s(step_time), "bssid": bssid, "ssid": ssid, }) for bssid, ssid in wifis - known_wifis: # new wifi push_message( "wifi_analyzer", { "event": "new_wifi", "timestamp": d2s(step_time), "bssid": bssid, "ssid": ssid, }) known_wifis = wifis
def redis_add_stop_msg(): push_message( "output", { "event": "stop", "timestamp": d2s(utcnow()), }, )
def redis_add_boot_msg(): push_message( "output", { "event": "boot", "timestamp": d2s(utcnow()), # add context information here, like a boot counter and version etc }, )
def runner(): for _, step_time in timed_loop(5, name="fakegps"): # we should probably open /dev/ttyXX the serial of # the gps ship and parse the output push_message("wifi_analyzer", { "event": "new_position", "timestamp": d2s(step_time), "latitude": 50.819658, "longitude": 4.398903, })
def runner(): current_position = None while True: message = get_message("wifi_analyzer") if message["event"] == "new_position": current_position = (message["latitude"], message["longitude"]) if message["event"] == "new_wifi" and current_position: push_message("output", { "event": "new_wifi", "timestamp": message["timestamp"], "latitude": current_position[0], "longitude": current_position[1], "bssid": message["bssid"], "ssid": message["ssid"], })
def runner(): start_time = monotonic() start_dt = utcnow() redis = get_redis() for _, step_time in timed_loop(10, name="diagnostic"): # ram info, let's parse /proc/meminfo mems = open("/proc/meminfo").read().split("\n")[:5] mems = [l[:-3].replace(" ", "").split(":") for l in mems] mems = {k: int(v) for k, v in mems} # disk, redis, loadavg rootfs = statvfs("/") r_info = redis.info() queue_size = redis.llen("output") loadavg = open("/proc/loadavg").read().split(" ")[:3] # clock derivation runtime_dt = utcnow() - start_dt runtime = monotonic() - start_time derivation = runtime - runtime_dt.total_seconds() uptime = float(open("/proc/uptime").read().split(" ")[0]) # error counters log_name = [k.decode() for k in redis.keys("log_*")] if log_name: log_counts = [int(c) for c in redis.mget(log_name)] logs = dict(zip(log_name, log_counts)) else: logs = {} # eth1 tx/rx stats try: path = "/sys/class/net/wlan0/statistics/" target = ["tx_bytes", "tx_packets", "rx_bytes", "rx_packets"] wlan0 = {t: int(open(path + t).read()) for t in target} except: wlan0 = {} info = { "event": "board_status", "timestamp": d2s(step_time), # disk/nand status "rootfs_blocks": rootfs.f_bsize * rootfs.f_blocks, "rootfs_free": rootfs.f_bsize * rootfs.f_bfree, "rootfs_available": rootfs.f_bsize * rootfs.f_bavail, # redis info "redis_size": r_info["used_memory"], "redis_size_peak": r_info["used_memory_peak"], "redis_ops": r_info["instantaneous_ops_per_sec"], # load "loadavg1": float(loadavg[0]), "loadavg5": float(loadavg[1]), "loadavg15": float(loadavg[2]), # ram info "mem_total": mems["MemTotal"], "mem_free": mems["MemFree"], "mem_available": mems["MemAvailable"], "mem_buffers": mems["Buffers"], "mem_cached": mems["Cached"], # connection "queue_size": queue_size, # clock "derivation": derivation, "uptime": floor(uptime), } info.update(wlan0) info.update(logs) push_message("output", info)