import socket import time import argparse METRIC_NAME = 'test.metric.from.python' def send_metric(ip, port, metric_name, value): timestamp = int(time.time()) message = f"{metric_name} {value} {timestamp}\n" print(f"Sending: {message.strip()} to {ip}:{port}") try: sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect((ip, port)) sock.sendall(message.encode('utf-8')) sock.close() print("Metric sent successfully.") except Exception as e: print(f"Error sending metric: {e}") if __name__ == "__main__": parser = argparse.ArgumentParser( description='Send test metrics to a Graphite server.', epilog='Example:\n python your_script.py --ip 192.168.1.100 --port 2003', formatter_class=argparse.RawTextHelpFormatter ) parser.add_argument('--ip', type=str, required=True, help='Graphite server IP address') parser.add_argument('--port', type=int, default=2003, help='Graphite server port (default: 2003)') args = parser.parse_args() for i in range(5): send_metric(args.ip, args.port, METRIC_NAME, i * 10 + 50) time.sleep(2)