Add localserver, systemd service file
parent
9dd64d1da5
commit
2d65aa7ec7
@ -0,0 +1,98 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/webmeisterei/lql_api/lql"
|
||||||
|
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
localServerMinConns := 0
|
||||||
|
localServerMaxConns := 0
|
||||||
|
localServerCmd.Flags().IntVarP(&localServerMinConns, "min-conns", "m", 2, "minimal Client Connections")
|
||||||
|
localServerCmd.Flags().IntVarP(&localServerMaxConns, "max-conns", "x", 5, "maximal Client Connections")
|
||||||
|
|
||||||
|
localServerCmd.Flags().StringP("socket", "s", "/opt/omd/sites/{site}/tmp/run/live", "Socket")
|
||||||
|
localServerCmd.Flags().StringP("htpasswd", "t", "/opt/omd/sites/{site}/etc/htpasswd", "htpasswd file")
|
||||||
|
localServerCmd.Flags().BoolP("debug", "d", false, "Enable Debug on stderr")
|
||||||
|
localServerCmd.Flags().StringP("listen", "l", ":8080", "Address to listen on")
|
||||||
|
rootCmd.AddCommand(localServerCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
var localServerCmd = &cobra.Command{
|
||||||
|
Use: "localserver [site]",
|
||||||
|
Short: "Local LQL Server",
|
||||||
|
Long: `Local LQL Server
|
||||||
|
|
||||||
|
Requires a local lql unix socket.`,
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
sReplacer := strings.NewReplacer("{site}", args[0])
|
||||||
|
logger := log.New()
|
||||||
|
logger.SetOutput(os.Stderr)
|
||||||
|
if !cmd.Flag("debug").Changed {
|
||||||
|
logger.SetLevel(log.InfoLevel)
|
||||||
|
} else {
|
||||||
|
logger.SetLevel(log.TraceLevel)
|
||||||
|
}
|
||||||
|
|
||||||
|
socket, err := cmd.Flags().GetString("socket")
|
||||||
|
if err != nil {
|
||||||
|
logger.WithField("error", err).Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
localSocket := sReplacer.Replace(socket)
|
||||||
|
var lqlClient *lql.Client
|
||||||
|
|
||||||
|
logger.WithFields(log.Fields{"localSocket": localSocket}).Debug("Sockets")
|
||||||
|
|
||||||
|
sigc := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(sigc, os.Interrupt, os.Kill, syscall.SIGTERM)
|
||||||
|
go func(c chan os.Signal) {
|
||||||
|
// Wait for a SIGINT or SIGKILL:
|
||||||
|
sig := <-c
|
||||||
|
logger.WithFields(log.Fields{"signal": sig}).Info("Caught signal shutting down.")
|
||||||
|
|
||||||
|
// Stop listening (and unlink the socket if unix type):
|
||||||
|
if lqlClient != nil {
|
||||||
|
lqlClient.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
os.Exit(1)
|
||||||
|
}(sigc)
|
||||||
|
|
||||||
|
minConns, err := cmd.Flags().GetInt("min-conns")
|
||||||
|
if err != nil {
|
||||||
|
logger.WithField("error", err).Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
maxConns, err := cmd.Flags().GetInt("max-conns")
|
||||||
|
if err != nil {
|
||||||
|
logger.WithField("error", err).Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
lqlClient, err = lql.NewClient(minConns, maxConns, "unix", localSocket)
|
||||||
|
if err != nil {
|
||||||
|
logger.WithField("error", err).Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer lqlClient.Close()
|
||||||
|
lqlClient.SetLogger(logger)
|
||||||
|
|
||||||
|
htpasswd := sReplacer.Replace(cmd.Flag("htpasswd").Value.String())
|
||||||
|
server, err := lql.NewServer(lqlClient, logger, htpasswd)
|
||||||
|
if err != nil {
|
||||||
|
logger.WithField("error", err).Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
server.ListenAndServe(cmd.Flag("listen").Value.String())
|
||||||
|
},
|
||||||
|
}
|
@ -1,14 +1,18 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
set -ex
|
ARGS=$2
|
||||||
|
|
||||||
SERVER="http://localhost:8080"
|
SERVER="http://localhost:8080"
|
||||||
|
if [ -n "$1" ]; then
|
||||||
|
SERVER=$1
|
||||||
|
fi
|
||||||
|
|
||||||
|
set -ex
|
||||||
|
|
||||||
# GET Hosts
|
# GET Hosts
|
||||||
curl -X POST -d '{"method": "GET", "table": "hosts", "columns": ["name", "address", "groups"]}' $SERVER/v1/raw
|
curl $2 -X POST -d '{"method": "GET", "table": "hosts", "columns": ["name", "address", "groups"]}' $SERVER/v1/raw
|
||||||
|
|
||||||
# GET Hosts with limit
|
# GET Hosts with limit
|
||||||
curl -X POST -d '{"method": "GET", "table": "hosts", "columns": ["name", "address", "groups"], "limit": 3}' $SERVER/v1/raw
|
curl $2 -X POST -d '{"method": "GET", "table": "hosts", "columns": ["name", "address", "groups"], "limit": 3}' $SERVER/v1/raw
|
||||||
|
|
||||||
# host stats from the tactical_overview widget
|
# host stats from the tactical_overview widget
|
||||||
curl -X POST -d '{"method": "GET", "table": "hosts", "query": [["Stats", "state >= 0"], ["Stats", "state > 0"], ["Stats", "scheduled_downtime_depth = 0"], ["StatsAnd", "2"], ["Stats", "state > 0"], ["Stats", "scheduled_downtime_depth = 0"], ["Stats", "acknowledged = 0"], ["StatsAnd", "3"], ["Stats", "host_staleness >= 1.5"], ["Stats", "host_scheduled_downtime_depth = 0"], ["StatsAnd", "2"]]}' $SERVER/v1/raw
|
curl $2 -X POST -d '{"method": "GET", "table": "hosts", "query": [["Stats", "state >= 0"], ["Stats", "state > 0"], ["Stats", "scheduled_downtime_depth = 0"], ["StatsAnd", "2"], ["Stats", "state > 0"], ["Stats", "scheduled_downtime_depth = 0"], ["Stats", "acknowledged = 0"], ["StatsAnd", "3"], ["Stats", "host_staleness >= 1.5"], ["Stats", "host_scheduled_downtime_depth = 0"], ["StatsAnd", "2"]]}' $SERVER/v1/raw
|
@ -0,0 +1,12 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=LQL API Server
|
||||||
|
After=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=%i
|
||||||
|
Group=%i
|
||||||
|
ExecStart=/usr/local/bin/lql_api localserver %i --listen localhost:8080 -d
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
Loading…
Reference in New Issue