starting prometheus alerter
This commit is contained in:
parent
90caa81998
commit
8f477a0c79
@ -35,6 +35,29 @@ type configPrometheus struct {
|
||||
NumGroupDeleted prometheus.Gauge
|
||||
}
|
||||
|
||||
type alertmanagerStruct struct {
|
||||
Version string `json:"version"`
|
||||
GroupKey string `json:"groupKey"`
|
||||
TruncatedAlerts int `json:"truncatedAlerts"`
|
||||
Status string `json:"status"`
|
||||
Reciver string `json:"receiver"`
|
||||
GroupLabels string `json:"groupLabels"`
|
||||
ComminLabels string `json:"commonLabels"`
|
||||
CommonAnnotaations string `json:"commonAnnotations"`
|
||||
ExternalURL string `json:"externalURL"`
|
||||
Alerts []alertmanagerAlertsStruct `json:"alerts"`
|
||||
}
|
||||
|
||||
type alertmanagerAlertsStruct struct {
|
||||
Status string `json:"status"`
|
||||
Labels string `json:"labels"`
|
||||
Annotations string `json:"annotations"`
|
||||
StartsAt string `json:"startsAt"`
|
||||
EndsAt string `json:"endsAt"`
|
||||
GeneratorURL string `json:"generatorURL"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
}
|
||||
|
||||
var config = configStructure{
|
||||
TimeFormat: "2006-01-02 15:04:05",
|
||||
Log: &logutils.LevelFilter{
|
||||
|
@ -1,18 +1,13 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"net/http"
|
||||
|
||||
"tplink/internal/tplink"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
@ -83,133 +78,13 @@ func webTPLink(w http.ResponseWriter, r *http.Request) {
|
||||
crossSiteOrigin(w)
|
||||
|
||||
if strings.ToLower(r.Method) == "get" {
|
||||
keys := r.URL.Query()
|
||||
if len(keys) == 0 {
|
||||
log.Printf("[INFO] Required parameters missing: no host, state, or deviceid was provided.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameters missing: no parameter for host, state, or deviceid was provided.")
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
host string
|
||||
hostSet bool
|
||||
state bool
|
||||
stateSet bool
|
||||
id int
|
||||
)
|
||||
|
||||
for k, v := range keys {
|
||||
log.Printf("%s: %v\n", k, v)
|
||||
switch strings.ToLower(k) {
|
||||
case "host":
|
||||
if len(v) != 1 {
|
||||
log.Printf("[INFO] Host was defined multiple times.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameter host was defined multiple times.")
|
||||
return
|
||||
}
|
||||
_, err := net.LookupHost(v[0])
|
||||
if err != nil {
|
||||
log.Printf("[INFO] Unable to resolve provided hostname: %s\n", v[0])
|
||||
tmpltError(w, http.StatusBadRequest, "Unable to resolve provided hostname.")
|
||||
return
|
||||
}
|
||||
hostSet = true
|
||||
host = v[0]
|
||||
|
||||
case "state":
|
||||
if len(v) != 1 {
|
||||
log.Printf("[INFO] State was defined multiple times.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameter state was defined multiple times.")
|
||||
return
|
||||
}
|
||||
s, err := strconv.ParseBool(v[0])
|
||||
if err != nil {
|
||||
log.Printf("[INFO] State is not boolean: %s\n", v[0])
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameter state is not boolean.")
|
||||
return
|
||||
}
|
||||
stateSet = true
|
||||
state = s
|
||||
|
||||
case "deviceid":
|
||||
if len(v) != 1 {
|
||||
log.Printf("[INFO] Deviceid was defined multiple times.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameter device was defined multiple times.")
|
||||
return
|
||||
}
|
||||
d, err := strconv.Atoi(v[0])
|
||||
if err != nil {
|
||||
log.Printf("[INFO] Deviceid is not an integer: %s\n", v[0])
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameter deviceid is not an integer.")
|
||||
return
|
||||
}
|
||||
id = d
|
||||
}
|
||||
}
|
||||
|
||||
if !hostSet {
|
||||
log.Printf("[INFO] Required parameter missing: no host was provided.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameters missing: no parameter for host provided.")
|
||||
return
|
||||
}
|
||||
if !stateSet {
|
||||
log.Printf("[INFO] Required parameter missing: no state was provided.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameters missing: no parameter for state provided.")
|
||||
return
|
||||
}
|
||||
|
||||
t := tplink.Tplink{
|
||||
Host: host,
|
||||
SwitchID: id,
|
||||
}
|
||||
if err := t.ChangeStateMultiSwitch(state); err != nil {
|
||||
log.Printf("[INFO] Unable to change device state: %v\n", err)
|
||||
tmpltError(w, http.StatusInternalServerError, fmt.Sprintf("Unable to change device state: %v\n", err))
|
||||
return
|
||||
}
|
||||
|
||||
sysInfo, err := t.SystemInfo()
|
||||
if err != nil {
|
||||
log.Printf("[INFO] Unable to get info from target device: %v\n", err)
|
||||
tmpltError(w, http.StatusInternalServerError, fmt.Sprintf("Unable to read info from device: %v\n", err))
|
||||
return
|
||||
}
|
||||
|
||||
var ds int
|
||||
if len(sysInfo.System.GetSysinfo.Children) == 0 {
|
||||
ds = sysInfo.System.GetSysinfo.RelayState
|
||||
} else {
|
||||
ds = sysInfo.System.GetSysinfo.Children[id].State
|
||||
}
|
||||
|
||||
var fs string
|
||||
if ds == 0 {
|
||||
fs = "OFF"
|
||||
} else {
|
||||
fs = "ON"
|
||||
}
|
||||
|
||||
o := struct {
|
||||
Status string `json:"status" yaml:"status"`
|
||||
Host string `json:"host" yaml:"host"`
|
||||
DeviceID int `json:"device_id" yaml:"deviceID"`
|
||||
}{
|
||||
Status: fs,
|
||||
Host: host,
|
||||
DeviceID: id,
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
output, err := json.MarshalIndent(o, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("[TRACE] Unable to marshal error message: %v.", err)
|
||||
}
|
||||
w.Write(output) //nolint:errcheck
|
||||
|
||||
tmpltTPlinkGet(w, r)
|
||||
} else if strings.ToLower(r.Method) == "post" {
|
||||
tmpltTPlinkPost(w, r)
|
||||
} else if strings.ToLower(r.Method) == "options" {
|
||||
return
|
||||
} else {
|
||||
log.Printf("[DEBUG] Request to '%s' was made using the wrong method: expected %s, got %s", "GET|OPTIONS", r.URL.Path, strings.ToUpper(r.Method))
|
||||
log.Printf("[DEBUG] Request to '%s' was made using the wrong method: expected %s, got %s", "GET|POST|OPTIONS", r.URL.Path, strings.ToUpper(r.Method))
|
||||
tmpltError(w, http.StatusBadRequest, "Invalid http method.")
|
||||
}
|
||||
}
|
||||
|
@ -2,8 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"tplink/internal/tplink"
|
||||
)
|
||||
|
||||
func tmpltError(w http.ResponseWriter, s int, m string) {
|
||||
@ -65,3 +70,139 @@ func tmpltHealthCheck(w http.ResponseWriter) {
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Write(output) //nolint:errcheck
|
||||
}
|
||||
|
||||
func tmpltTPlinkGet(w http.ResponseWriter, r *http.Request) {
|
||||
keys := r.URL.Query()
|
||||
if len(keys) == 0 {
|
||||
log.Printf("[INFO] Required parameters missing: no host, state, or deviceid was provided.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameters missing: no parameter for host, state, or deviceid was provided.")
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
host string
|
||||
hostSet bool
|
||||
state bool
|
||||
stateSet bool
|
||||
id int
|
||||
)
|
||||
|
||||
for k, v := range keys {
|
||||
switch strings.ToLower(k) {
|
||||
case "host":
|
||||
if len(v) != 1 {
|
||||
log.Printf("[INFO] Host was defined multiple times.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameter host was defined multiple times.")
|
||||
return
|
||||
}
|
||||
_, err := net.LookupHost(v[0])
|
||||
if err != nil {
|
||||
log.Printf("[INFO] Unable to resolve provided hostname: %s\n", v[0])
|
||||
tmpltError(w, http.StatusBadRequest, "Unable to resolve provided hostname.")
|
||||
return
|
||||
}
|
||||
hostSet = true
|
||||
host = v[0]
|
||||
|
||||
case "state":
|
||||
if len(v) != 1 {
|
||||
log.Printf("[INFO] State was defined multiple times.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameter state was defined multiple times.")
|
||||
return
|
||||
}
|
||||
s, err := strconv.ParseBool(v[0])
|
||||
if err != nil {
|
||||
log.Printf("[INFO] State is not boolean: %s\n", v[0])
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameter state is not boolean.")
|
||||
return
|
||||
}
|
||||
stateSet = true
|
||||
state = s
|
||||
|
||||
case "deviceid":
|
||||
if len(v) != 1 {
|
||||
log.Printf("[INFO] Deviceid was defined multiple times.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameter device was defined multiple times.")
|
||||
return
|
||||
}
|
||||
d, err := strconv.Atoi(v[0])
|
||||
if err != nil {
|
||||
log.Printf("[INFO] Deviceid is not an integer: %s\n", v[0])
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameter deviceid is not an integer.")
|
||||
return
|
||||
}
|
||||
id = d
|
||||
}
|
||||
}
|
||||
|
||||
if !hostSet {
|
||||
log.Printf("[INFO] Required parameter missing: no host was provided.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameters missing: no parameter for host provided.")
|
||||
return
|
||||
}
|
||||
if !stateSet {
|
||||
log.Printf("[INFO] Required parameter missing: no state was provided.\n")
|
||||
tmpltError(w, http.StatusBadRequest, "Required parameters missing: no parameter for state provided.")
|
||||
return
|
||||
}
|
||||
|
||||
t := tplink.Tplink{
|
||||
Host: host,
|
||||
SwitchID: id,
|
||||
}
|
||||
if err := t.ChangeStateMultiSwitch(state); err != nil {
|
||||
log.Printf("[INFO] Unable to change device state: %v\n", err)
|
||||
tmpltError(w, http.StatusInternalServerError, fmt.Sprintf("Unable to change device state: %v\n", err))
|
||||
return
|
||||
}
|
||||
|
||||
sysInfo, err := t.SystemInfo()
|
||||
if err != nil {
|
||||
log.Printf("[INFO] Unable to get info from target device: %v\n", err)
|
||||
tmpltError(w, http.StatusInternalServerError, fmt.Sprintf("Unable to read info from device: %v\n", err))
|
||||
return
|
||||
}
|
||||
|
||||
var ds int
|
||||
if len(sysInfo.System.GetSysinfo.Children) == 0 {
|
||||
ds = sysInfo.System.GetSysinfo.RelayState
|
||||
} else {
|
||||
ds = sysInfo.System.GetSysinfo.Children[id].State
|
||||
}
|
||||
|
||||
var fs string
|
||||
if ds == 0 {
|
||||
fs = "OFF"
|
||||
} else {
|
||||
fs = "ON"
|
||||
}
|
||||
|
||||
o := struct {
|
||||
Status string `json:"status" yaml:"status"`
|
||||
Host string `json:"host" yaml:"host"`
|
||||
DeviceID int `json:"device_id" yaml:"deviceID"`
|
||||
}{
|
||||
Status: fs,
|
||||
Host: host,
|
||||
DeviceID: id,
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
|
||||
output, err := json.MarshalIndent(o, "", " ")
|
||||
if err != nil {
|
||||
log.Printf("[TRACE] Unable to marshal error message: %v.", err)
|
||||
}
|
||||
w.Write(output) //nolint:errcheck
|
||||
}
|
||||
|
||||
func tmpltTPlinkPost(w http.ResponseWriter, r *http.Request) {
|
||||
var payload alertmanagerStruct
|
||||
|
||||
err := json.NewDecoder(r.Body).Decode(&payload)
|
||||
if err != nil {
|
||||
log.Printf("[INFO] Unable to parse payload from API call: %v\n", err)
|
||||
log.Printf("[INFO] Data received: %#v\n", payload)
|
||||
tmpltError(w, http.StatusInternalServerError, fmt.Sprintf("Unable to read info from device: %v\n", err))
|
||||
return
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user