Skip to content

Commit

Permalink
Added scrape for status endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Breidenstein committed Oct 1, 2021
1 parent ee84a42 commit db7ee42
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 41 deletions.
36 changes: 26 additions & 10 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

type Exporter struct {
Hostname string
accessKey string
AccessKey string
}

func NewExporter(hostname string, accessKey string) *Exporter {
return &Exporter{
Hostname: hostname,
accessKey: accessKey,
AccessKey: accessKey,
}
}

Expand All @@ -27,6 +27,9 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- mqttServerStatus
ch <- freeMemory
ch <- freeDiskSpace
ch <- systemStatus
ch <- loggedErrors
ch <- redundancyStatus
}

func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
Expand All @@ -42,15 +45,15 @@ func (e *Exporter) Scrape(ch chan<- prometheus.Metric) float64 {
errors := 0

// Get alarm inputs
inputResponse, err := QueryInputs(e.Hostname, e.accessKey)
inputResponse, err := QueryInputs(e.Hostname, e.AccessKey)
if err != nil {
fmt.Println(err)
errors += 1
} else {
for _, input := range *inputResponse {
for _, state := range []string{"OK", "ERROR", "NOT_USED"} {
ch <- prometheus.MustNewConstMetric(
inputStatus, prometheus.GaugeValue, input.HasStatus(state), input.Name, input.Identifier, state,
inputStatus, prometheus.GaugeValue, CheckState(input.State, state), input.Name, input.Identifier, state,
)
}
}
Expand All @@ -66,40 +69,53 @@ func (e *Exporter) Scrape(ch chan<- prometheus.Metric) float64 {
}

// Get cloud services
serviceResponse, err := QueryCloudServices(e.Hostname, e.accessKey)
serviceResponse, err := QueryCloudServices(e.Hostname, e.AccessKey)
if err != nil {
fmt.Println(err)
errors += 1
} else {
for _, service := range *serviceResponse {
for _, state := range []string{"OK", "ERROR"} {
ch <- prometheus.MustNewConstMetric(
cloudServiceStatus, prometheus.GaugeValue, service.HasStatus(state), service.Name, state,
cloudServiceStatus, prometheus.GaugeValue, CheckState(service.State, state), service.Name, state,
)
}
}
}

// System status
// TODO
statusResponse, err := QueryStatus(e.Hostname, e.AccessKey)
if err != nil {
fmt.Println(err)
errors += 1
} else {
ch <- prometheus.MustNewConstMetric(loggedErrors, prometheus.GaugeValue, statusResponse.NbrOfLoggedErrors)

for _, state := range []string{"OK", "WARN", "ERROR"} {
ch <- prometheus.MustNewConstMetric(systemStatus, prometheus.GaugeValue, CheckState(statusResponse.State, state), state)
}
for _, state := range []string{"OK", "WARN"} {
ch <- prometheus.MustNewConstMetric(redundancyStatus, prometheus.GaugeValue, CheckState(statusResponse.RedundancyState.State, state), state)
}
}

// MQTT status
mqttResponse, err := QueryMQTTServer(e.Hostname, e.accessKey)
mqttResponse, err := QueryMQTTServer(e.Hostname, e.AccessKey)
if err != nil {
fmt.Println(err)
errors += 1
} else {
for _, server := range *mqttResponse {
for _, state := range []string{"OK", "ERROR", "NOT_USED"} {
ch <- prometheus.MustNewConstMetric(
mqttServerStatus, prometheus.GaugeValue, server.HasStatus(state), server.Name, state,
mqttServerStatus, prometheus.GaugeValue, CheckState(server.State, state), server.Name, state,
)
}
}
}

// Storage/Memory status
systemReponse, err := QuerySystem(e.Hostname, e.accessKey)
systemReponse, err := QuerySystem(e.Hostname, e.AccessKey)
if err != nil {
fmt.Println(err)
errors += 1
Expand Down
15 changes: 15 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ var (
"Free space on storage disks",
[]string{"drive_letter"}, nil,
)
systemStatus = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "system_status"),
"Current state of the system",
[]string{"state"}, nil,
)
loggedErrors = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "logged_errors"),
"Number of errors in the last 60 minutes",
nil, nil,
)
redundancyStatus = prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "redundancy_status"),
"Current redundancy state of the system",
[]string{"state"}, nil,
)
)

func main() {
Expand Down
76 changes: 45 additions & 31 deletions rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,28 @@ type MQTTServer struct {
State string
}

type SystemStatus struct {
type SystemResponse struct {
FreeMemory float64 `json:"freeMemory"`
Disks []struct {
DriveLetter string `json:"disk"`
FreeSpace float64 `json:"freeSpace"`
} `json:"disks"`
}

// HasStatus checks if the alarm input has the given state
func (i InputDetail) HasStatus(status string) float64 {
if i.State == status {
return 1
} else {
return 0
}
type StatusResponse struct {
State string `json:"state"`
Message string `json:"message"`
NbrOfLoggedErrors float64 `json:"nbrOfLoggedErrors"`
RedundancyState struct {
State string `json:"state"`
Current string `json:"current"`
Configured string `json:"configured"`
} `json:"redundancyState"`
}

// HasStatus checks if the cloudservice has the given state
func (c CloudService) HasStatus(status string) float64 {
if c.State == status {
return 1
} else {
return 0
}
}

// HasStatus checks if the cloudservice has the given state
func (m MQTTServer) HasStatus(status string) float64 {
if m.State == status {
// CheckState compares two string values and returns either 0 oder 1
func CheckState(value string, state string) float64 {
if value == state {
return 1
} else {
return 0
Expand All @@ -86,11 +79,11 @@ func (i InputDetail) GetValue() (float64, error) {
}
}

// QueryInputs returns a list with informations about all alarm inputs
func QueryInputs(hostname string, accessKey string) (*[]InputDetail, error) {
// QueryInputs returns a list with information about all alarm inputs
func QueryInputs(hostname string, AccessKey string) (*[]InputDetail, error) {
authHeader := req.Header{
"Accept": "application/json",
"Authorization": accessKey,
"Authorization": AccessKey,
}
var inputDetails []InputDetail
var inputOverview InputOverview
Expand Down Expand Up @@ -125,10 +118,10 @@ func QueryInputs(hostname string, accessKey string) (*[]InputDetail, error) {
}

// QueryCloudServices returns a list of all cloudservices
func QueryCloudServices(hostname string, accessKey string) (*[]CloudService, error) {
func QueryCloudServices(hostname string, AccessKey string) (*[]CloudService, error) {
authHeader := req.Header{
"Accept": "application/json",
"Authorization": accessKey,
"Authorization": AccessKey,
}
var services []CloudService

Expand All @@ -146,10 +139,10 @@ func QueryCloudServices(hostname string, accessKey string) (*[]CloudService, err
}

// QueryMQTTServer returns a list of all mqtt brokers
func QueryMQTTServer(hostname string, accessKey string) (*[]MQTTServer, error) {
func QueryMQTTServer(hostname string, AccessKey string) (*[]MQTTServer, error) {
authHeader := req.Header{
"Accept": "application/json",
"Authorization": accessKey,
"Authorization": AccessKey,
}
var resp MQTTRestResponse

Expand Down Expand Up @@ -179,13 +172,13 @@ func QueryMQTTServer(hostname string, accessKey string) (*[]MQTTServer, error) {
return &mqttServer, nil
}

// QuerySystem returns informations about memory and disc space
func QuerySystem(hostname string, accessKey string) (*SystemStatus, error) {
// QuerySystem returns information about memory and disc space
func QuerySystem(hostname string, AccessKey string) (*SystemResponse, error) {
authHeader := req.Header{
"Accept": "application/json",
"Authorization": accessKey,
"Authorization": AccessKey,
}
var resp SystemStatus
var resp SystemResponse

r, err := req.Get(fmt.Sprintf("%s/rest/monitoring/system", hostname), authHeader)
if err != nil {
Expand All @@ -210,3 +203,24 @@ func QuerySystem(hostname string, accessKey string) (*SystemStatus, error) {

return &resp, nil
}

// QueryStatus returns status information about the FE2 software
func QueryStatus(hostname string, AccessKey string) (*StatusResponse, error) {
authHeader := req.Header{
"Accept": "application/json",
"Authorization": AccessKey,
}
var resp StatusResponse

r, err := req.Get(fmt.Sprintf("%s/rest/monitoring/status", hostname), authHeader)
if err != nil {
return nil, err
}

err = r.ToJSON(&resp)
if err != nil {
return nil, err
}

return &resp, nil
}

0 comments on commit db7ee42

Please sign in to comment.