#!/usr/bin/python3

# Copyright (c) 2026, Oracle and/or its affiliates.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, see <https://www.gnu.org/licenses/>.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.

"""
kmon_notify

Purpose:
  Receives event data from dtrace (or another event producer) and performs two actions:
    1) Writes the latest event as a JSON record to:
         /var/run/oled/kmon/events/<event>
       The file is overwritten on each invocation so consumers can watch the file
       for the most recent event notification.
    2) Notifies kmon_server over a Unix domain socket so it can take further action.

Command-line arguments:
  argv[1]: event  Event name (also used as the event file name)
  argv[2]: pid    Process ID associated with the event
  argv[3]: cpu    CPU identifier associated with the event

Outputs:
  - File write (overwrite): /var/run/oled/kmon/events/<event>
      Content: one JSON line with keys EVENT, TIME, PID, CPU
  - Socket notification to kmon_server:
      Sends a message like: "EVENT=...,PID=...,CPU=..."

Notes:
  - SOCKET_PATH must match the path used by kmon_server.
  - The BASE_PATH directory must exist and be writable.
"""

import sys
import time
import socket
import json

# Must match the Unix domain socket path used by kmon_server
SOCKET_PATH = "/var/run/oled/kmon/kmon_socket"

# Event directory; the final file path is: /var/run/oled/kmon/events/<event>
BASE_PATH = "/var/run/oled/kmon/events/"

def act():
    """
        - Persist the latest event to a per-event file (JSON, overwrite)
        - Notify kmon_server via Unix domain socket
    """

    event = sys.argv[1]
    pid = int(sys.argv[2])
    cpu = int(sys.argv[3])

    file_path = BASE_PATH + event
    ts = int(time.time())  # Unix epoch timestamp in seconds

    # Overwrite the event file so watchers can read the most recent event
    json_msg = json.dumps({"EVENT": event, "TIME": ts, "PID": pid, "CPU": cpu}) + "\n"
    with open(file_path, "w", encoding="utf-8") as f:
        f.write(json_msg)

    # Notify kmon_server for further processing
    sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
    sock.connect(SOCKET_PATH)
    msg = f"EVENT={event},PID={pid},CPU={cpu}"
    sock.send(msg.encode())
    sock.close()

act()
