This repository was archived by the owner on Mar 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim2.py
More file actions
executable file
·100 lines (79 loc) · 2.39 KB
/
sim2.py
File metadata and controls
executable file
·100 lines (79 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/python3
#
# Copyright (C) 2021, Transpara LLC. All rights reserved.
#
# simulate values for a bunch of tags
#
# February 4, 2022 - Inselbuch
# from older work
# writes to tstore using tstore-api
# backfill code converted to regular loop
# hardcoded for one-minute values
# includes hostname to support data from multiple hosts
# takes promURL from environment variable (or defaults)
# no error checking on tagfile syntax
#
#
from datetime import datetime,timedelta
import time
import math
import random
import gc
import os
import sys
import socket
import json
import requests
import traceback
hostname = socket.gethostname()
headers = {'Content-Type': 'application/json'}
points = {}
f = open('tagfile.txt','r')
for line in f:
ls = line.split(',')
tagname = ls[0]
typical = ls[1]
points[tagname]=typical
f.close()
# use the hostname passed in from the host through an env variable in the run command
host_name = os.getenv('host_name',hostname)
# when running inside a container (on the same network as tstore-api)
promURL = os.getenv('promURL','http://tstore-api:80/api/v1/write')
# promscale does not like certain characters
# and there are others that I don't like
def promName(c):
x = c.lower()
x = x.replace(' ','_')
x = x.replace('.','_')
x = x.replace('/','_')
return x
# convert time to promscale expectation
def epochInt(etime):
return int(etime*1000.0)
def send(labels,samples):
data = {"labels" : labels, "samples" : samples}
r=requests.request(method='POST',url=promURL,headers=headers,data=json.dumps(data),verify=False,timeout=10)
# for backfilling
#end_time = datetime.now()
#start_time = end_time - timedelta(days=10)\
#ts = start_time
#while ts < end_time:
# for regular operation
ts = datetime.now()
while True:
# FOR DAILY VALUES ONLY AT MIDNIGHT
#x = ts.replace(hour=0,minute=0,second=0,microsecond=0)
for p in points:
tagname = p
typical = float(points[p])
#v = random.random()*typical
v = typical * 0.95 + random.random()*typical*0.10
now = epochInt(datetime.now().timestamp())
labels = {"__name__": 'carbon','hostname':host_name,'tagname':tagname }
samples = [[now,v]]
send(labels,samples)
# for backfilling
# ts = ts + timedelta(minutes=5)
#time.sleep(0.1)
# for regular operation (new value twice a day)
time.sleep(43200)