Skip to content

Commit

Permalink
Clm hostname fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tommaso-ascani committed Jun 25, 2024
1 parent 48384e5 commit d44647a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions imageroot/actions/set-clm-forwarder/10set
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#

import subprocess
import hashlib
import agent
import json
import uuid
Expand All @@ -23,6 +24,7 @@ if not bool(subscription):

if (request['active']):
agent.set_env('CLOUD_LOG_MANAGER_UUID', uuid.uuid5(uuid.NAMESPACE_DNS, cluster_uuid))
agent.set_env('CLOUD_LOG_MANAGER_HOSTNAME', 'cluster-' + hashlib.sha256(cluster_uuid.encode("utf-8")).hexdigest()[:8])
agent.set_env('CLOUD_LOG_MANAGER_ADDRESS', f"{request['address']}")
agent.set_env('CLOUD_LOG_MANAGER_TENANT', f"{request['tenant']}")

Expand Down
15 changes: 11 additions & 4 deletions imageroot/bin/cloud-log-manager-forwarder
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ import sys

try:
UUID = os.environ.get('CLOUD_LOG_MANAGER_UUID')
HOSTNAME = os.environ.get('CLOUD_LOG_MANAGER_HOSTNAME')
START_TIME = os.environ.get('CLOUD_LOG_MANAGER_START_TIME', '')
ADDRESS = os.environ.get('CLOUD_LOG_MANAGER_ADDRESS', 'https://nar.nethesis.it')
TENANT = os.environ.get('CLOUD_LOG_MANAGER_TENANT')

HOSTNAME = os.popen('hostname').read().rstrip('\n')

os.environ['LOKI_ADDR'] = f"http://127.0.0.1:{os.environ['LOKI_HTTP_PORT']}"
os.environ['LOKI_USERNAME'] = os.environ['LOKI_API_AUTH_USERNAME']
os.environ['LOKI_PASSWORD'] = os.environ['LOKI_API_AUTH_PASSWORD']
Expand All @@ -46,6 +45,9 @@ def send_logs_list(logs_list: list, last_timestamp: str):
session.post(ADDRESS + "/adm/syslog/noauth_put_json/", verify=True, data=json.dumps(logs_list))
session.close()

with open("./logs_info.txt", "a") as tf:
tf.write('Log inviati: ' + str(len(logs_list)) + str(f' ({sys.getsizeof(logs_list)} bytes)\n'))

# Write on file the last log sent timestamp
with open("./cloud_log_manager_last_timestamp", "w") as tf:
tf.write(last_timestamp)
Expand Down Expand Up @@ -89,6 +91,11 @@ while True:
# LogCLI command
logcli_command = ["logcli", "query", "--limit", "0", "--forward", "--timezone", "UTC", "--from", last_timestamp, "--no-labels", "-q", "-o", "jsonl",
'{node_id=~".+"} | json severity="PRIORITY", priority="SYSLOG_IDENTIFIER", pid="_PID", message="MESSAGE" | line_format "<{{.priority}}> [{{.node_id}}:{{.module_id}}:{{.process}}] {{.process}}[{{.pid}}]: {{.message}}"']
with open("./logs_info.txt", "a") as tf:
temp = ''
for elem in logcli_command:
temp += elem + ' '
tf.write(temp + '\n')
with subprocess.Popen(logcli_command, stdout=subprocess.PIPE) as logs:
for log in logs.stdout.readlines():
try:
Expand Down Expand Up @@ -120,8 +127,8 @@ while True:
# Save log timestamp only when socket send succeed
last_timestamp = json_object["timestamp"]

# If list size is bigger than 1 MB, send
if sys.getsizeof(logs_list) >= 1048576:
# If list size is bigger than 128 kB, send
if sys.getsizeof(logs_list) >= 131072:
send_logs_list(logs_list, last_timestamp)
logs_list.clear()
except Exception as exc:
Expand Down
6 changes: 6 additions & 0 deletions imageroot/events/default-instance-changed/10set
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import os
import sys
import datetime
import subprocess
import hashlib

import agent

Expand Down Expand Up @@ -40,6 +41,8 @@ if os.getenv("AGENT_EVENT_SOURCE") == 'cluster':
stderr=sys.stderr,
text=True,
check=True)

agent.unset_env('CLOUD_LOG_MANAGER_HOSTNAME')

# New instance
if 'current' in data and data['current'] == os.getenv('MODULE_ID'):
Expand All @@ -49,7 +52,10 @@ if os.getenv("AGENT_EVENT_SOURCE") == 'cluster':

# If cloud log manager service was active in old instance, start on new instacnce
if all(key in previous_environment for key in ('CLOUD_LOG_MANAGER_ADDRESS', 'CLOUD_LOG_MANAGER_TENANT', 'CLOUD_LOG_MANAGER_UUID')) and bool(subscription):
cluster_uuid = rdb.get('cluster/uuid')

agent.set_env('CLOUD_LOG_MANAGER_UUID', previous_environment['CLOUD_LOG_MANAGER_UUID'])
agent.set_env('CLOUD_LOG_MANAGER_HOSTNAME', 'cluster-' + hashlib.sha256(cluster_uuid.encode("utf-8")).hexdigest()[:8])
agent.set_env('CLOUD_LOG_MANAGER_ADDRESS', previous_environment['CLOUD_LOG_MANAGER_ADDRESS'])
agent.set_env('CLOUD_LOG_MANAGER_TENANT', previous_environment['CLOUD_LOG_MANAGER_TENANT'])

Expand Down
Empty file modified imageroot/events/subscription-changed/10check_forwarder
100644 → 100755
Empty file.

0 comments on commit d44647a

Please sign in to comment.