gomonit

package module
v0.0.0-...-ceda698 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2021 License: MIT Imports: 15 Imported by: 0

README

Codacy Badge Coverage Status

GoMonit Golang API for Monit

Monit is a free open source utility for managing and monitoring, processes, programs, files, directories and filesystems on a UNIX system. Monit conducts automatic maintenance and repair and can execute meaningful causal actions in error situations.

Monit Source Code

Monit Tildeslash official website

Simple golang agent API for Monit. Can be used to perform some simple action on Monit daemon :

  • retrieve current status,
  • start a service,
  • stop a service
  • activate / deactivate monitoring on a service

Usage

go get http://github.com/Axili39/gomonit

Prerequisites

Activate http in /etc/monit/monitrc config file :

  • specify a bind address,
  • specify admin/password credential
set httpd port 2812 and
     use address 127.0.0.1
     allow admin:monit

Note: see /etc/monit/monitrc config file instructions, for more informations

Examples

package main
import (
	"fmt"	
	"encoding/json"
	"github.com/Axili39/gomonit"
)

func main() {
	agent, err := gomonit.NewMonitAgent("http://127.0.0.1:2812", "admin:monit")
	if err != nil {
		fmt.Printf("%v\n",err)
		return
	}

	// Show current status
	out,_ := json.Marshal(&agent.Status)
	fmt.Printf("%s\n",string(out))

	// Do an action on service
	err = agent.StartService("foo")
	if err != nil {
		fmt.Printf("%v\n",err)
		return
	}

	// Request Status
	err = agent.RequestStatus()
	if err != nil {
		fmt.Printf("%v\n",err)
		return
	}

	out,_ = json.Marshal(&agent.Status)
	fmt.Printf("%s\n",string(out))

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MonitAgent

type MonitAgent struct {
	Auth       bool   // Authentication needed
	AuthString string // Authentication info ex: admin:monit
	URL        string // Monit httpd url, can be unix:///var/run/monit.sock, httpd://<host>:<port>

	Status *MonitStatus // Last Status Obtained from Monit
	// contains filtered or unexported fields
}

MonitAgent Agent class that can :

  • retrieve status from Monit Daemon
  • Invoke Action on service (start / stop / monitor / unmonitor)

func NewMonitAgent

func NewMonitAgent(url string, authString string) (*MonitAgent, error)

NewMonitAgent Create new MonitAgent instance and automatically try to connect to daemon and retrieve current status.

func (*MonitAgent) CmdService

func (m *MonitAgent) CmdService(service string, action string) error

CmdService Perform command on service

func (*MonitAgent) RequestStatus

func (m *MonitAgent) RequestStatus() error

RequestStatus Requests Monit daemon to retrieve current status

func (*MonitAgent) StartService

func (m *MonitAgent) StartService(service string) error

StartService perform monit start <service> command

func (*MonitAgent) StopService

func (m *MonitAgent) StopService(service string) error

StopService perform monit stop <service> command

type MonitStatus

type MonitStatus struct {
	XMLName  xml.Name  `xml:"monit" json:"-"`
	Server   Server    `xml:"server" json:"server"`
	Platform Platform  `xml:"platform" json:"platform"`
	Services []Service `xml:"service" json:"service"`
}

MonitStatus This structure embedded all data returned by monit daemon

func (*MonitStatus) GetService

func (r *MonitStatus) GetService(name string) *Service

GetService : Get Service info from Status

func (*MonitStatus) Load

func (r *MonitStatus) Load(file io.Reader) error

Load : Load MonitStatus from io.Reader

func (*MonitStatus) Print

func (r *MonitStatus) Print(w io.Writer)

Print : Dump Status to File (json format)

type NetStatElem

type NetStatElem struct {
	Now   int64 `xml:"now" json:"now"`
	Total int64 `xml:"total" json:"total"`
}

NetStatElem parts of NetStats

type NetStats

type NetStats struct {
	Packet *NetStatElem `xml:"packets" json:"packets"`
	Bytes  *NetStatElem `xml:"bytes" json:"bytes"`
	Errors *NetStatElem `xml:"errors" json:"errors"`
}

NetStats Link network statistics

type Platform

type Platform struct {
	Memory  int64  `xml:"memory" json:"memory"`
	Swap    int64  `xml:"swap" json:"swap"`
	Name    string `xml:"name" json:"name"`
	Release string `xml:"release" json:"release"`
	Version string `xml:"version" json:"version"`
	Machine string `xml:"machine" json:"machine"`
	CPU     int    `xml:"cpu" json:"cpu"`
}

Platform "platform" part of MonitStatus

type Server

type Server struct {
	Hostname    string `xml:"localhostname" json:"localhostname"`
	ControlFile string `xml:"controlfile" json:"controlfile"`
	HTTPd       struct {
		Address string `xml:"address" json:"address"`
		Port    int    `xml:"port" json:"port"`
		SSL     int    `xml:"ssl" json:"ssl"`
	} `xml:"httpd" json:"httpd"`
	Incarnation int64  `xml:"incarnation" json:"incarnation"`
	Version     string `xml:"version" json:"version"`
	Uptime      int    `xml:"uptime" json:"uptime"`
	ID          string `xml:"id" json:"id"`
	Poll        int    `xml:"poll" json:"poll"`
	StartDelay  int    `xml:"startdelay" json:"startdelay"`
}

Server "server" part of MonitStatus

type Service

type Service struct {
	Type          int    `xml:"type,attr" json:"type"`
	Name          string `xml:"name" json:"name"`
	CollectedUsec int    `xml:"collected_usec" json:"collected_usec"`
	Status        int    `xml:"status" json:"status"`
	MonitorMode   int    `xml:"monitormode" json:"monitormode"`
	Pid           int    `xml:"pid" json:"pid"`
	PPid          *int   `xml:"ppid" json:"ppid,omitempty"`
	UID           *int   `xml:"uid" json:"uid,omitempty"`
	EUID          *int   `xml:"euid" json:"euid,omitempty"`
	GID           *int   `xml:"gid,omitempty" json:"gid,omitempty"`
	Uptime        int    `xml:"uptime" json:"uptime"`
	Threads       int    `xml:"threads" json:"threads"`
	Children      int    `xml:"children" json:"children"`
	StatusHint    int    `xml:"status_hint" json:"status_hint"`
	OnReboot      int    `xml:"onreboot" json:"onreboot"`
	CollectedSec  int    `xml:"collected_sec" json:"collected_sec"`
	Monitor       int    `xml:"monitor" json:"monitor"`
	PendingAction int    `xml:"pendingaction" json:"pendingaction"`
	CPU           *struct {
		Percent      float32 `xml:"percent" json:"percent"`
		PercentTotal float32 `xml:"percenttotal" json:"percenttotal"`
	} `xml:"cpu,omitempty" json:"cpu,omitempty"`
	//	TODO		<read></read>			<write></write>
	Memory *struct {
		Percent        float32 `xml:"percent" json:"percent"`
		PercentTotal   float32 `xml:"percenttotal" json:"percenttotal"`
		KiloBytes      int64   `xml:"kilobyte" json:"kilobyte"`
		KiloBytesTotal int64   `xml:"kilobytetotal" json:"kilobytetotal"`
	} `xml:"memory" json:"memory,omitempty"`
	System *struct {
		Swap struct {
			Percent   float32 `xml:"percent" json:"percent"`
			KiloBytes int64   `xml:"kilobyte" json:"kilobyte"`
		} `xml:"swap" json:"swap"`
		Load struct {
			Avg01 float32 `xml:"avg01" json:"avg01"`
			Avg05 float32 `xml:"avg05" json:"avg05"`
			Avg15 float32 `xml:"avg15" json:"avg15"`
		} `xml:"load" json:"load"`
		CPU struct {
			User   float32 `xml:"user" json:"user"`
			System float32 `xml:"system" json:"system"`
			Wait   float32 `xml:"wait" json:"wait"`
		} `xml:"cpu" json:"cpu"`
		Memory struct {
			Percent   float32 `xml:"percent" json:"percent"`
			KiloBytes int64   `xml:"kilobyte" json:"kilobyte"`
		} `xml:"memory" json:"memory"`
	} `xml:"system" json:"system,omitempty"`
	Link *struct {
		State    int       `xml:"state" json:"state"`
		Speed    int64     `xml:"speed" json:"speed"`
		Duplex   int       `xml:"duplex" json:"duplex"`
		Download *NetStats `xml:"download" json:"download"`
		Upload   *NetStats `xml:"upload" json:"upload"`
	} `xml:"link" json:"link,omitempty"`
}

Service service entry which contains service status

func (*Service) Print

func (s *Service) Print(w io.Writer)

Print : Dump Service info to File (json format)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL