Skip to content

Commit d853dba

Browse files
authored
Merge pull request #19 from wbollock/feat/users
feat: user metrics
2 parents 9e6c4e5 + 78d04c3 commit d853dba

File tree

1 file changed

+72
-2
lines changed

1 file changed

+72
-2
lines changed

nagios_exporter.go

+72-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const servicestatusAPI = "/objects/servicestatus"
2929
const systeminfoAPI = "/system/info"
3030
const systemstatusAPI = "/system/status"
3131
const systemstatusDetailAPI = "/system/statusdetail"
32+
const systemuserAPI = "/system/user"
3233

3334
type systemStatus struct {
3435
// https://stackoverflow.com/questions/21151765/cannot-unmarshal-string-into-go-value-of-type-int64
@@ -107,6 +108,15 @@ type serviceStatus struct {
107108
} `json:"servicestatus"`
108109
}
109110

111+
type userStatus struct {
112+
// yes, this field is named records even though every other endpoint is `recordcount`...
113+
Recordcount int64 `json:"records"`
114+
Userstatus []struct {
115+
Admin int64 `json:"admin,string"`
116+
Enabled int64 `json:"enabled,string"`
117+
} `json:"users"`
118+
}
119+
110120
func ReadConfig(configPath string) Config {
111121

112122
var conf Config
@@ -153,6 +163,11 @@ var (
153163
// technically there is no such thing as a check_type of passive for these metrics
154164
hostchecksPerformance = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "host_checks_performance_seconds"), "Host checks performance", []string{"check_type", "performance_type", "operator"}, nil)
155165
servicechecksPerformance = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "service_checks_performance_seconds"), "Service checks performance", []string{"check_type", "performance_type", "operator"}, nil)
166+
167+
// Users
168+
usersTotal = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "users_total"), "Amount of users present on the system", nil, nil)
169+
usersPrivileges = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "users_privileges_total"), "Amount of admin or regular users", []string{"privileges"}, nil)
170+
usersStatus = prometheus.NewDesc(prometheus.BuildFQName(namespace, "", "users_status_total"), "Amount of disabled or enabled users", []string{"status"}, nil)
156171
)
157172

158173
type Exporter struct {
@@ -193,6 +208,10 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
193208
ch <- servicechecks
194209
ch <- hostchecksPerformance
195210
ch <- servicechecksPerformance
211+
// Users
212+
ch <- usersTotal
213+
ch <- usersPrivileges
214+
ch <- usersStatus
196215
}
197216

198217
func (e *Exporter) TestNagiosConnectivity(sslVerify bool, nagiosAPITimeout time.Duration) float64 {
@@ -392,7 +411,7 @@ func (e *Exporter) QueryAPIsAndUpdateMetrics(ch chan<- prometheus.Metric, sslVer
392411
servicesTotal, prometheus.GaugeValue, float64(serviceStatusObject.Recordcount),
393412
)
394413

395-
var servicesCount, servicessCheckedCount, servicesScheduledCount, servicesActiveCheckCount,
414+
var servicesCount, servicesCheckedCount, servicesScheduledCount, servicesActiveCheckCount,
396415
servicesPassiveCheckCount, servicesOkCount, servicesWarnCount, servicesCriticalCount,
397416
servicesUnknownCount, servicesFlapCount, servicesDowntimeCount, servicesProblemsAcknowledgedCount int
398417

@@ -401,7 +420,7 @@ func (e *Exporter) QueryAPIsAndUpdateMetrics(ch chan<- prometheus.Metric, sslVer
401420
servicesCount++
402421

403422
if v.HasBeenChecked == 0 {
404-
servicessCheckedCount++
423+
servicesCheckedCount++
405424
}
406425

407426
if v.ShouldBeScheduled == 0 {
@@ -581,6 +600,57 @@ func (e *Exporter) QueryAPIsAndUpdateMetrics(ch chan<- prometheus.Metric, sslVer
581600
servicechecksPerformance, prometheus.GaugeValue, float64(systemStatusDetailObject.Nagioscore.Activeservicecheckperf.MinExecutionTime), "active", "execution", "max",
582601
)
583602

603+
// user information
604+
// we also need to tack on the optional parameter of `advanced` to get privilege information
605+
systemUserURL := e.nagiosEndpoint + systemuserAPI + "?apikey=" + e.nagiosAPIKey + "&advanced=1"
606+
607+
body = QueryAPIs(systemUserURL, sslVerify, nagiosAPITimeout)
608+
log.Debug("Queried API: ", systemuserAPI)
609+
610+
userStatusObject := userStatus{}
611+
612+
jsonErr = json.Unmarshal(body, &userStatusObject)
613+
if jsonErr != nil {
614+
log.Fatal(jsonErr)
615+
}
616+
617+
var usersAdminCount, usersRegularCount, usersEnabledCount, usersDisabledCount int
618+
619+
ch <- prometheus.MustNewConstMetric(
620+
usersTotal, prometheus.GaugeValue, float64(userStatusObject.Recordcount),
621+
)
622+
623+
for _, v := range userStatusObject.Userstatus {
624+
625+
if v.Admin == 1 {
626+
usersAdminCount++
627+
} else {
628+
usersRegularCount++
629+
}
630+
631+
if v.Enabled == 1 {
632+
usersEnabledCount++
633+
} else {
634+
usersDisabledCount++
635+
}
636+
}
637+
638+
ch <- prometheus.MustNewConstMetric(
639+
usersStatus, prometheus.GaugeValue, float64(usersEnabledCount), "enabled",
640+
)
641+
642+
ch <- prometheus.MustNewConstMetric(
643+
usersStatus, prometheus.GaugeValue, float64(usersDisabledCount), "disabled",
644+
)
645+
646+
ch <- prometheus.MustNewConstMetric(
647+
usersPrivileges, prometheus.GaugeValue, float64(usersAdminCount), "admin",
648+
)
649+
650+
ch <- prometheus.MustNewConstMetric(
651+
usersPrivileges, prometheus.GaugeValue, float64(usersRegularCount), "user",
652+
)
653+
584654
log.Info("Endpoint scraped and metrics updated")
585655
}
586656

0 commit comments

Comments
 (0)