Skip to content

Commit

Permalink
Merge pull request #61 from pitan/master
Browse files Browse the repository at this point in the history
メンテナンス情報のみ取得できるオプションを追加
  • Loading branch information
yamamoto-febc authored Jan 11, 2022
2 parents b4317cc + d65d017 commit b388d02
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 127 deletions.
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,28 @@ $ docker run -p 9542:9542 -e SAKURACLOUD_ACCESS_TOKEN=<YOUR-TOKEN> -e SAKURACLOU

### Flags

| Flag / Environment Variable | Required | Default | Description |
| -------------------------------------------- | -------- | ---------- | ------------------------- |
| `--token` / `SAKURACLOUD_ACCESS_TOKEN` || | API Key(Token) |
| `--secret` / `SAKURACLOUD_ACCESS_TOKEN_SECRET` || | API Key(Secret) |
| `--ratelimit`/ `SAKURACLOUD_RATE_LIMIT` | | `5` | API request rate limit(maximum:10) |
| `--webaddr` / `WEB_ADDR` | | `:9542` | Exporter's listen address |
| `--webpath`/ `WEB_PATH` | | `/metrics` | Metrics request path |
| `--no-collector.auto-backup` | | `false` | Disable the AutoBackup collector |
| `--no-collector.coupon` | | `false` | Disable the Coupon collector |
| `--no-collector.database` | | `false` | Disable the Database collector |
| `--no-collector.esme` | | `false` | Disable the ESME collector |
| `--no-collector.internet` | | `false` | Disable the Internet(Switch+Router) collector |
| `--no-collector.load-balancer` | | `false` | Disable the LoadBalancer collector |
| `--no-collector.local-router` | | `false` | Disable the LocalRouter collector |
| `--no-collector.mobile-gateway` | | `false` | Disable the MobileGateway collector |
| `--no-collector.nfs` | | `false` | Disable the NFS collector |
| `--no-collector.proxy-lb` | | `false` | Disable the ProxyLB(Enhanced LoadBalancer) collector |
| `--no-collector.server` | | `false` | Disable the Server collector |
| `--no-collector.sim` | | `false` | Disable the SIM collector |
| `--no-collector.vpc-router` | | `false` | Disable the VPCRouter collector |
| `--no-collector.zone` | | `false` | Disable the Zone collector |
| Flag / Environment Variable | Required | Default | Description |
| -------------------------------------------- | -------- | ---------- | ------------------------- |
| `--token` / `SAKURACLOUD_ACCESS_TOKEN` || | API Key(Token) |
| `--secret` / `SAKURACLOUD_ACCESS_TOKEN_SECRET` || | API Key(Secret) |
| `--ratelimit`/ `SAKURACLOUD_RATE_LIMIT` | | `5` | API request rate limit(maximum:10) |
| `--webaddr` / `WEB_ADDR` | | `:9542` | Exporter's listen address |
| `--webpath`/ `WEB_PATH` | | `/metrics` | Metrics request path |
| `--no-collector.auto-backup` | | `false` | Disable the AutoBackup collector |
| `--no-collector.coupon` | | `false` | Disable the Coupon collector |
| `--no-collector.database` | | `false` | Disable the Database collector |
| `--no-collector.esme` | | `false` | Disable the ESME collector |
| `--no-collector.internet` | | `false` | Disable the Internet(Switch+Router) collector |
| `--no-collector.load-balancer` | | `false` | Disable the LoadBalancer collector |
| `--no-collector.local-router` | | `false` | Disable the LocalRouter collector |
| `--no-collector.mobile-gateway` | | `false` | Disable the MobileGateway collector |
| `--no-collector.nfs` | | `false` | Disable the NFS collector |
| `--no-collector.proxy-lb` | | `false` | Disable the ProxyLB(Enhanced LoadBalancer) collector |
| `--no-collector.server` | | `false` | Disable the Server collector |
| `--no-collector.server.except-maintenance` | | `false` | Disable the Server collector except for maintenance information |
| `--no-collector.sim` | | `false` | Disable the SIM collector |
| `--no-collector.vpc-router` | | `false` | Disable the VPCRouter collector |
| `--no-collector.zone` | | `false` | Disable the Zone collector |


#### Flags for debug
Expand Down
182 changes: 93 additions & 89 deletions collector/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import (

// ServerCollector collects metrics about all servers.
type ServerCollector struct {
ctx context.Context
logger log.Logger
errors *prometheus.CounterVec
client iaas.ServerClient
ctx context.Context
logger log.Logger
errors *prometheus.CounterVec
client iaas.ServerClient
maintOnly bool

Up *prometheus.Desc
ServerInfo *prometheus.Desc
Expand All @@ -57,7 +58,7 @@ type ServerCollector struct {
}

// NewServerCollector returns a new ServerCollector.
func NewServerCollector(ctx context.Context, logger log.Logger, errors *prometheus.CounterVec, client iaas.ServerClient) *ServerCollector {
func NewServerCollector(ctx context.Context, logger log.Logger, errors *prometheus.CounterVec, client iaas.ServerClient, maintenanceOnly bool) *ServerCollector {
errors.WithLabelValues("server").Add(0)

serverLabels := []string{"id", "name", "zone"}
Expand All @@ -69,10 +70,11 @@ func NewServerCollector(ctx context.Context, logger log.Logger, errors *promethe
maintenanceInfoLabel := append(serverLabels, "info_url", "info_title", "description", "start_date", "end_date")

return &ServerCollector{
ctx: ctx,
logger: logger,
errors: errors,
client: client,
ctx: ctx,
logger: logger,
errors: errors,
client: client,
maintOnly: maintenanceOnly,
Up: prometheus.NewDesc(
"sakuracloud_server_up",
"If 1 the server is up and running, 0 otherwise",
Expand Down Expand Up @@ -195,105 +197,107 @@ func (c *ServerCollector) Collect(ch chan<- prometheus.Metric) {

serverLabels := c.serverLabels(server)

var up float64
if server.InstanceStatus.IsUp() {
up = 1.0
}
ch <- prometheus.MustNewConstMetric(
c.Up,
prometheus.GaugeValue,
up,
serverLabels...,
)
ch <- prometheus.MustNewConstMetric(
c.ServerInfo,
prometheus.GaugeValue,
float64(1.0),
c.serverInfoLabels(server)...,
)
ch <- prometheus.MustNewConstMetric(
c.CPUs,
prometheus.GaugeValue,
float64(server.GetCPU()),
serverLabels...,
)
ch <- prometheus.MustNewConstMetric(
c.Memories,
prometheus.GaugeValue,
float64(server.GetMemoryGB()),
serverLabels...,
)

// maintenance info
var maintenanceScheduled float64
if server.InstanceHostInfoURL != "" {
maintenanceScheduled = 1.0
wg.Add(1)
go func() {
c.collectMaintenanceInfo(ch, server)
wg.Done()
}()
}
ch <- prometheus.MustNewConstMetric(
c.MaintenanceScheduled,
prometheus.GaugeValue,
maintenanceScheduled,
serverLabels...,
)

wg.Add(len(server.Disks))
for i := range server.Disks {
go func(i int) {
c.collectDiskInfo(ch, server, i)
wg.Done()
}(i)
}

for i := range server.Interfaces {
if !c.maintOnly {
var up float64
if server.InstanceStatus.IsUp() {
up = 1.0
}
ch <- prometheus.MustNewConstMetric(
c.Up,
prometheus.GaugeValue,
up,
serverLabels...,
)
ch <- prometheus.MustNewConstMetric(
c.NICInfo,
c.ServerInfo,
prometheus.GaugeValue,
float64(1.0),
c.nicInfoLabels(server, i)...,
c.serverInfoLabels(server)...,
)

bandwidth := float64(server.BandWidthAt(i))
ch <- prometheus.MustNewConstMetric(
c.NICBandwidth,
c.CPUs,
prometheus.GaugeValue,
bandwidth,
c.nicLabels(server, i)...,
float64(server.GetCPU()),
serverLabels...,
)
ch <- prometheus.MustNewConstMetric(
c.Memories,
prometheus.GaugeValue,
float64(server.GetMemoryGB()),
serverLabels...,
)
}

if server.InstanceStatus.IsUp() {
// collect metrics per resources under server
now := time.Now()
// CPU-TIME
wg.Add(1)
go func() {
c.collectCPUTime(ch, server, now)
wg.Done()
}()

// Disks
wg.Add(len(server.Disks))
for i := range server.Disks {
go func(i int) {
c.collectDiskMetrics(ch, server, i, now)
c.collectDiskInfo(ch, server, i)
wg.Done()
}(i)
}

// NICs
wg.Add(len(server.Interfaces))
for i := range server.Interfaces {
go func(i int) {
c.collectNICMetrics(ch, server, i, now)
ch <- prometheus.MustNewConstMetric(
c.NICInfo,
prometheus.GaugeValue,
float64(1.0),
c.nicInfoLabels(server, i)...,
)

bandwidth := float64(server.BandWidthAt(i))
ch <- prometheus.MustNewConstMetric(
c.NICBandwidth,
prometheus.GaugeValue,
bandwidth,
c.nicLabels(server, i)...,
)
}

if server.InstanceStatus.IsUp() {
// collect metrics per resources under server
now := time.Now()
// CPU-TIME
wg.Add(1)
go func() {
c.collectCPUTime(ch, server, now)
wg.Done()
}(i)
}()

// Disks
wg.Add(len(server.Disks))
for i := range server.Disks {
go func(i int) {
c.collectDiskMetrics(ch, server, i, now)
wg.Done()
}(i)
}

// NICs
wg.Add(len(server.Interfaces))
for i := range server.Interfaces {
go func(i int) {
c.collectNICMetrics(ch, server, i, now)
wg.Done()
}(i)
}
}
}

// maintenance info
var maintenanceScheduled float64
if server.InstanceHostInfoURL != "" {
maintenanceScheduled = 1.0
wg.Add(1)
go func() {
c.collectMaintenanceInfo(ch, server)
wg.Done()
}()
}
ch <- prometheus.MustNewConstMetric(
c.MaintenanceScheduled,
prometheus.GaugeValue,
maintenanceScheduled,
serverLabels...,
)
}(servers[i])
}

Expand Down
Loading

0 comments on commit b388d02

Please sign in to comment.