-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create actual daemon service to execute the log sensor
- Loading branch information
Showing
4 changed files
with
109 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
#!/bin/bash | ||
|
||
python3 tikuna/sensor/logs_sensor.py | ||
python3 tikuna/sensor/sensor_daemon.py ${1} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import docker | ||
import re | ||
import json | ||
import requests | ||
import os | ||
from os.path import join, dirname | ||
from dotenv import load_dotenv | ||
from threading import Thread, Event | ||
|
||
dotenv_path = join(dirname(__file__), '.env') | ||
load_dotenv(dotenv_path) | ||
client = docker.from_env() | ||
|
||
indices = (6,7,8,9,10) | ||
ansi_escape = re.compile(r'\x1B(?:[@-Z\\-_]|\[[0-?]*[ -/]*[@-~])') | ||
|
||
TIKUNA_SERVER_URL = os.environ.get("TIKUNA_SERVER_URL") | ||
|
||
class LogSensor(threading.Thread): | ||
|
||
def __init__(self, client): | ||
self.dkg = client.containers.get(client).logs(stream = True, follow = True, tail = 10) | ||
|
||
def start_data_stream(self): | ||
try: | ||
print("Starting %s log collection..." % client) | ||
logs_added = 0 | ||
log_list = [] | ||
while True: | ||
line = next(dkg).decode("utf-8") | ||
if "Tikuna log" in line and "removed" in line: | ||
line = ansi_escape.sub('', line) | ||
words = line.split() | ||
log = [words[0]] | ||
log.extend([ words[i].partition('=')[2] for i in indices ]) | ||
if logs_added > 20: | ||
logs_added = 0 | ||
jsonString = json.dumps(log_list) | ||
log_list = [] | ||
print("Sending log request...") | ||
print(TIKUNA_SERVER_URL) | ||
try: | ||
r = requests.post(TIKUNA_SERVER_URL, json=jsonString) | ||
print(f"Status Code: {r.status_code}, Response: {r}") | ||
except: | ||
print("Connection error!") | ||
else: | ||
log_list.append(log) | ||
logs_added += 1 | ||
except StopIteration: | ||
print(f'log stream ended for prysm-beacon') | ||
|
||
def run(self): | ||
self.start_data_stream() | ||
|
||
def stop(self): | ||
pass |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import sys, time | ||
import logging | ||
import socket | ||
|
||
from daemon import Daemon | ||
from queue import Queue, Empty | ||
from tikuna.sensor.log_sensor import LogSensor | ||
|
||
# The tikuna daemon service that collects security | ||
# monitoring information. | ||
|
||
# TODO: this should be configured by files | ||
data_path = "/var/lib/tikuna/data" | ||
log_path = "/var/log/tikuna/" | ||
log_file = "%s/%s-tikuna-sensor.log" % (log_path, socket.gethostname()) | ||
pid_file = "/var/lib/tikuna/tikuna-sensor.pid" | ||
|
||
class SensorDaemon(Daemon): | ||
|
||
def run(self): | ||
logging.basicConfig(filename=log_file, | ||
level=logging.INFO) | ||
logging.info('Creating tikuna client services...') | ||
log_sensor_service = tikunaSensors(None) | ||
# Start the services. | ||
logging.info('Starting the tikuna client services...') | ||
log_sensor_service.start_sensors() | ||
logging.info('tikuna service client started...') | ||
|
||
def stop(self): | ||
# TODO: do something here? | ||
logging.info('Service tikuna stopped ...') | ||
|
||
if __name__ == "__main__": | ||
daemon = SensorDaemon( pid_file, | ||
stdout=log_file, | ||
stderr=log_file) | ||
if len(sys.argv) == 2: | ||
if 'start' == sys.argv[1]: | ||
daemon.start() | ||
elif 'stop' == sys.argv[1]: | ||
daemon.stop() | ||
elif 'restart' == sys.argv[1]: | ||
daemon.restart() | ||
else: | ||
print("Unknown command") | ||
sys.exit(2) | ||
sys.exit(0) | ||
else: | ||
print("usage: %s start|stop|restart" % sys.argv[0]) | ||
sys.exit(2) |