Skip to content
Snippets Groups Projects
Select Git revision
2 results Searching

main.go

Blame
  • main.go 6.17 KiB
    package main
    
    import (
    	"flag"
    	"net/http"
    	"github.com/prometheus/client_golang/prometheus/promhttp"
    	"fmt"
    	"os"
    	"github.com/prometheus/common/log"
    	"github.com/prometheus/common/version"
    	"github.com/prometheus/client_golang/prometheus"
    	"sync"
    	"crypto/tls"
    	"io/ioutil"
    	"encoding/json"
    	"bytes"
    )
    
    const (
    	namespace = "nexenta" // For Prometheus metrics.
    	apiPath   = "/rest/nms"
    )
    
    var (
    	listenAddr      = flag.String("listen-address", ":9457", "The address to listen on for HTTP requests.")
    	metricsEndpoint = flag.String("metrics-endpoint", "/metrics", "Path under which to expose metrics.")
    	apiHost         = flag.String("host", "nexenta", "Nexenta API host.")
    	apiPort         = flag.String("port", "8457", "Nexenta API port.")
    	apiUser         = flag.String("user", "admin", "Nexenta API username.")
    	apiPass         = flag.String("password", "password", "Nexenta API password.")
    	apiSsl          = flag.Bool("ssl", false, "Use SSL for the Nexenta API.")
    	insecure        = flag.Bool("insecure", false, "Ignore server certificate if using https.")
    	showVersion     = flag.Bool("version", false, "Print version information.")
    )
    
    type ApiResponse struct {
    	TGFlash string      `json:"tg_flash"`
    	Result  interface{} `json:"result"`
    	Error   interface{} `json:"error"`
    }
    
    type ResultObject struct {
    	State  []string `json:"state"`
    	Errors []string `json:"errors"`
    }
    
    type Exporter struct {
    	URI    string
    	mutex  sync.Mutex
    	client *http.Client
    
    	apiReachable float64
    
    	up             *prometheus.Desc
    	scrapeFailures  prometheus.Counter
    	volumeOnline   *prometheus.Desc
    }
    
    func NewExporter(uri string) *Exporter {
    	return &Exporter{
    		URI: uri,
    		up: prometheus.NewDesc(
    			prometheus.BuildFQName(namespace, "", "up"),
    			"Could the Nexenta API be reached",
    			nil,
    			nil),
    		scrapeFailures: prometheus.NewCounter(prometheus.CounterOpts{
    			Namespace: namespace,
    			Name:      "exporter_scrape_failures_total",
    			Help:      "Number of errors while scraping Nexenta API.",