Python Scripts: add script for sending test data to a Graphite instacne
This commit is contained in:
parent
9377f21977
commit
252abbf73c
1 changed files with 36 additions and 0 deletions
36
PythonScripts/send_graphite_data.py
Normal file
36
PythonScripts/send_graphite_data.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
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)
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue