Hi,
I’ve recently come across a problem with one of my telegraf input.exec:
2024-01-29T16:16:30Z E! [inputs.exec] Error in plugin: exec: exit status 1 for command '/etc/telegraf/scripts/netapp/OMCL_RMN_NetApp_VOl_nfs_forecasting.py': Traceback (most recent call last):...
If I run it in root commandline, it run well:
[root@qpr-sd-qosdb02 netapp]# python OMCL_RMN_NetApp_VOl_nfs_forecasting.py
predicted_capacity,week_number=6 value=70.9999961853
predicted_capacity,week_number=7 value=70.7199954987
predicted_capacity,week_number=8 value=70.439994812
predicted_capacity,week_number=9 value=70.1599941254
predicted_capacity,week_number=10 value=69.8799934387
predicted_capacity,week_number=11 value=69.5999927521
[root@qpr-sd-qosdb02 netapp]# echo $?
0
If I run it as the telegraf user, nothing happen and I get the following output:
[root@qpr-sd-qosdb02 netapp]# telegraf OMCL_RMN_NetApp_VOl_nfs_forecasting.py --test
2024-01-29T16:26:13Z I! Starting Telegraf 1.16.3
2024-01-29T16:26:13Z I! Using config file: /etc/telegraf/telegraf.conf
what is really odd is that if I run telegraf with --test or --once, I can see the output correctly, but on the second batch, I get the previous error.
Here is the complete script that I use:
#!/usr/bin/python
import requests
host = 'http://localhost:8086'
database = 'influxdb_script'
query = 'SELECT TOP("date",5),* FROM NetApp_VOl_nfs WHERE time > now() - 10m'
url = '{}/query?db={}&q={}'.format(host, database, query)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
if 'results' in data and 'series' in data['results'][0]:
series = data['results'][0]['series'][0]
week_index = series['columns'].index('week')
used_capacity_index = series['columns'].index('used_data')
wk = []
uc = []
for point in series['values']:
wk.append(point[week_index])
uc.append(point[used_capacity_index])
else:
print("Aucun resultat trouve dans la reponse.")
else:
print("Erreur lors de la requete. Code de statut:", response.status_code)
data = {
"week_number": [int(wk[0]), int(wk[1]), int(wk[2]), int(wk[3])],
"used_capacity": [float(uc[0]), float(uc[1]), float(uc[2]), float(uc[3])]
}
def linear_regression(x, y):
n = len(x)
x_mean = sum(x) / n
y_mean = sum(y) / n
numerator = sum((x[i] - x_mean) * (y[i] - y_mean) for i in range(n))
denominator = sum((x[i] - x_mean) ** 2 for i in range(n))
slope = numerator / denominator
intercept = y_mean - slope * x_mean
return slope, intercept
week_numbers = sorted(data["week_number"])
used_capacity_values = data["used_capacity"]
slope, intercept = linear_regression(week_numbers, used_capacity_values)
prediction_weeks = [ week_numbers[-1]+i for i in range(1, 7)]
predicted_capacity = [slope * week + intercept for week in prediction_weeks]
predicted_lines = [
"predicted_capacity,week_number={0} value={1}".format(prediction_weeks[i], predicted_capacity[i]) for i in range(len(prediction_weeks))
]
all_lines = predicted_lines
body = "\n".join(all_lines)
print(body)
Thank you !