check_nodes

This commit is contained in:
2021-04-08 12:02:23 +02:00
parent 7cee436446
commit e3b06b298c
3 changed files with 38 additions and 14 deletions
+35 -14
View File
@@ -9,6 +9,7 @@ import (
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
@@ -212,7 +213,7 @@ func InsertAlert(prefix string, deveui string, email string, threshold int) {
_, err := conn.Do("SET", prefix+deveui+":"+email, threshold)
if err != nil {
logit("InsertAlert: Error inserting: " + prefix + deveui + ":" + email)
logit("InsertAlert: Error inserting: " + prefix + deveui + ":" + email)
}
}
@@ -264,15 +265,32 @@ func CalcDaysUntil(mydate string) int {
return days
}
func vbat2percent(vbat string) int {
i, err := strconv.Atoi(vbat)
res := 0
if err == nil {
res = int(float64(i-3500) * float64((100.0/0.7)*0.001))
//fmt.Printf("vbat2percent Result in Percent: %d\n", res)
if res < 0 {
res = 0
} else if res > 100 {
res = 100
}
}
return res
}
func getLastMetrics(deveui string) OneMetric {
var res OneMetric
url := "http://localhost:8086/api/v2/query?org=wobischorg"
data := []byte(fmt.Sprintf(`from(bucket:"wobischbucket")
|> range(start:-5d)
|> tail(n:10)
|> filter(fn: (r) => r._measurement == "measurement" and r.deveui == "%s")
|> filter(fn: (r) => r._field == "vp")
|> last() |> yield(name: "last")`, deveui))
|> filter(fn: (r) => r._field == "vbat")
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")`, deveui))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
@@ -280,7 +298,9 @@ func getLastMetrics(deveui string) OneMetric {
}
// Set headers
req.Header.Set("Authorization", "Token nKYCoz3TA-LItYXG988DjdiStMhrfKmFXQqzxrjzJJ7Ek_iUttzFSE9lfe3s6q99EMdcrjuGlDAjp4Y0VnNRXw==")
var INFLUX_RO_TOKEN = os.Getenv("INFLUX_RO_TOKEN")
req.Header.Set("Authorization", "Token "+INFLUX_RO_TOKEN)
req.Header.Set("accept", "application/csv")
req.Header.Set("content-type", "application/vnd.flux")
@@ -307,19 +327,20 @@ func getLastMetrics(deveui string) OneMetric {
location, err := time.LoadLocation("Europe/Zurich")
for scanner.Scan() {
s := strings.Split(scanner.Text(), ",")
if (len(s) >= 7) && !(strings.HasPrefix(s[5], "_")) {
mytime, err := time.Parse(time.RFC3339, s[5])
if (len(s) >= 9) && !(strings.HasPrefix(s[3], "_")) {
t, err := time.Parse(time.RFC3339, s[3])
if err != nil {
fmt.Printf("error converting time: %s\n", s[3])
continue
}
res.Timestamp = mytime.In(location).Format("02.01.2006 15:04")
value := s[6]
field := s[7]
if field == "vp" {
res.BatteryPercent = value
}
res.Timestamp = t.In(location).Format("02.01.2006 15:04")
res.BatteryPercent = strconv.Itoa(vbat2percent(s[8]))
}
}
res.Deveui = deveui
res.Alias = getDevAlias(deveui)
res.ActiveUntil = getActiveUntil(deveui)
@@ -335,7 +356,7 @@ func CheckThreshold(d string, vp int, u2 string, last_metric OneMetric, info_thr
if vp <= alert_threshold {
if AlarmNotAlreadySent("alarm_sent_accu:", d, u2, alert_threshold) {
sendEmailAccu(u2, alias, d, last_metric.BatteryPercent, alert_threshold, "ALARM")
InsertAlert("alarm_sent_accu:", d , u2, alert_threshold)
InsertAlert("alarm_sent_accu:", d, u2, alert_threshold)
}
return false
}
@@ -371,7 +392,7 @@ func main() {
u2 := u[5:]
my_devs := getMyDevs(u2)
for _, d := range my_devs {
//fmt.Printf("%s:%s\n", u2, d)
fmt.Printf("%s:%s\n", u2, d)
if !strings.HasPrefix(d, "@") {
last_metric := getLastMetrics(d)
// Zuerst der Batteriealarm