check_nodes

This commit is contained in:
Joerg Lehmann 2021-04-08 12:02:23 +02:00
parent 7cee436446
commit e3b06b298c
3 changed files with 38 additions and 14 deletions

View File

@ -9,6 +9,7 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
"os"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -264,15 +265,32 @@ func CalcDaysUntil(mydate string) int {
return days 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 { func getLastMetrics(deveui string) OneMetric {
var res OneMetric var res OneMetric
url := "http://localhost:8086/api/v2/query?org=wobischorg" url := "http://localhost:8086/api/v2/query?org=wobischorg"
data := []byte(fmt.Sprintf(`from(bucket:"wobischbucket") data := []byte(fmt.Sprintf(`from(bucket:"wobischbucket")
|> range(start:-5d) |> range(start:-5d)
|> tail(n:10)
|> filter(fn: (r) => r._measurement == "measurement" and r.deveui == "%s") |> filter(fn: (r) => r._measurement == "measurement" and r.deveui == "%s")
|> filter(fn: (r) => r._field == "vp") |> filter(fn: (r) => r._field == "vbat")
|> last() |> yield(name: "last")`, deveui)) |> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")`, deveui))
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data)) req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil { if err != nil {
@ -280,7 +298,9 @@ func getLastMetrics(deveui string) OneMetric {
} }
// Set headers // 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("accept", "application/csv")
req.Header.Set("content-type", "application/vnd.flux") req.Header.Set("content-type", "application/vnd.flux")
@ -307,19 +327,20 @@ func getLastMetrics(deveui string) OneMetric {
location, err := time.LoadLocation("Europe/Zurich") location, err := time.LoadLocation("Europe/Zurich")
for scanner.Scan() { for scanner.Scan() {
s := strings.Split(scanner.Text(), ",") s := strings.Split(scanner.Text(), ",")
if (len(s) >= 7) && !(strings.HasPrefix(s[5], "_")) { if (len(s) >= 9) && !(strings.HasPrefix(s[3], "_")) {
mytime, err := time.Parse(time.RFC3339, s[5]) t, err := time.Parse(time.RFC3339, s[3])
if err != nil { if err != nil {
fmt.Printf("error converting time: %s\n", s[3])
continue continue
} }
res.Timestamp = mytime.In(location).Format("02.01.2006 15:04")
value := s[6] res.Timestamp = t.In(location).Format("02.01.2006 15:04")
field := s[7] res.BatteryPercent = strconv.Itoa(vbat2percent(s[8]))
if field == "vp" {
res.BatteryPercent = value
}
} }
} }
res.Deveui = deveui res.Deveui = deveui
res.Alias = getDevAlias(deveui) res.Alias = getDevAlias(deveui)
res.ActiveUntil = getActiveUntil(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 vp <= alert_threshold {
if AlarmNotAlreadySent("alarm_sent_accu:", d, u2, alert_threshold) { if AlarmNotAlreadySent("alarm_sent_accu:", d, u2, alert_threshold) {
sendEmailAccu(u2, alias, d, last_metric.BatteryPercent, alert_threshold, "ALARM") 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 return false
} }
@ -371,7 +392,7 @@ func main() {
u2 := u[5:] u2 := u[5:]
my_devs := getMyDevs(u2) my_devs := getMyDevs(u2)
for _, d := range my_devs { for _, d := range my_devs {
//fmt.Printf("%s:%s\n", u2, d) fmt.Printf("%s:%s\n", u2, d)
if !strings.HasPrefix(d, "@") { if !strings.HasPrefix(d, "@") {
last_metric := getLastMetrics(d) last_metric := getLastMetrics(d)
// Zuerst der Batteriealarm // Zuerst der Batteriealarm

1
go.mod
View File

@ -5,6 +5,7 @@ go 1.14
require ( require (
github.com/gomodule/redigo v1.8.4 github.com/gomodule/redigo v1.8.4
github.com/gorilla/securecookie v1.1.1 github.com/gorilla/securecookie v1.1.1
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible
github.com/stripe/stripe-go/v72 v72.41.0 github.com/stripe/stripe-go/v72 v72.41.0
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
) )

2
go.sum
View File

@ -4,6 +4,8 @@ github.com/gomodule/redigo v1.8.4/go.mod h1:P9dn9mFrCBvWhGE1wpxx6fgq7BAeLBk+UUUz
github.com/gomodule/redigo/redis v0.0.0-do-not-use h1:J7XIp6Kau0WoyT4JtXHT3Ei0gA1KkSc6bc87j9v9WIo= github.com/gomodule/redigo/redis v0.0.0-do-not-use h1:J7XIp6Kau0WoyT4JtXHT3Ei0gA1KkSc6bc87j9v9WIo=
github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ=
github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA=
github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=