gonit

package module
v0.0.0-...-9a8fd51 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2013 License: Apache-2.0 Imports: 30 Imported by: 0

README

gonit

gonit is currentlly an unfinished project that is intended to be a go replacement for monit. monit has some performance issues that caused this project's creation. gonit has not been run in cloud foundry production environments yet.

Documentation

Index

Constants

View Source
const (
	CONFIG_FILE_POSTFIX   = "-gonit.yml"
	SETTINGS_FILENAME     = "gonit.yml"
	UNIX_SOCKET_TRANSPORT = "unix_socket"
	MONITOR_MODE_ACTIVE   = "active"
	MONITOR_MODE_PASSIVE  = "passive"
	MONITOR_MODE_MANUAL   = "manual"
)
View Source
const (
	MONITOR_NOT     = 0x0
	MONITOR_YES     = 0x1
	MONITOR_INIT    = 0x2
	MONITOR_WAITING = 0x4
)
View Source
const (
	ACTION_START = iota
	ACTION_STOP
	ACTION_RESTART
	ACTION_MONITOR
	ACTION_UNMONITOR
)
View Source
const (
	DEFAULT_DURATION = "2s"
	DEFAULT_INTERVAL = "2s"
)
View Source
const (
	EQ_OPERATOR  = 0x1
	NEQ_OPERATOR = 0x2
	GT_OPERATOR  = 0x3
	LT_OPERATOR  = 0x4
)
View Source
const (
	INTERVAL_MARGIN_ERR = 0.05
	NANO_TO_MILLI       = float64(time.Millisecond)
)
View Source
const (
	CPU_PERCENT_NAME = "cpu_percent"
	MEMORY_USED_NAME = "memory_used"
)
View Source
const (
	DEFAULT_ALERT_TRANSPORT = "none"
)
View Source
const DEFAULT_ENV_PATH = "PATH=/bin:/usr/bin:/sbin:/usr/sbin"
View Source
const (
	ERROR_IN_PROGRESS_FMT = "Process %q action already in progress"
)
View Source
const (
	VERSION = "0.0.1"
)

Variables

View Source
var Log steno.Logger

Functions

func LookupGroupId

func LookupGroupId(group string) (int, error)

until we have user.LookupGroupId: http://codereview.appspot.com/4589049

func ReadPidFile

func ReadPidFile(path string) (int, error)

Read pid from a file

func RpcArgs

func RpcArgs(method string, name string, isGroup bool) (string, string)

helper to convert command-line arguments to api method call

func WritePidFile

func WritePidFile(pid int, path string) error

Write pid to a file

Types

type API

type API struct {
	Control *Control
}

func NewAPI

func NewAPI(config *ConfigManager) *API

func (*API) About

func (a *API) About(unused interface{}, about *About) error

server info

func (*API) MonitorAll

func (a *API) MonitorAll(unused interface{}, r *ActionResult) error

func (*API) MonitorGroup

func (a *API) MonitorGroup(name string, r *ActionResult) error

func (*API) MonitorProcess

func (a *API) MonitorProcess(name string, r *ActionResult) error

func (*API) Quit

func (a *API) Quit(unused interface{}, r *ActionResult) error

quit server daemon

func (*API) Reload

func (a *API) Reload(unused interface{}, r *ActionResult) error

reload server configuration

func (*API) RestartAll

func (a *API) RestartAll(unused interface{}, r *ActionResult) error

func (*API) RestartGroup

func (a *API) RestartGroup(name string, r *ActionResult) error

func (*API) RestartProcess

func (a *API) RestartProcess(name string, r *ActionResult) error

func (*API) StartAll

func (a *API) StartAll(unused interface{}, r *ActionResult) error

func (*API) StartGroup

func (a *API) StartGroup(name string, r *ActionResult) error

func (*API) StartProcess

func (a *API) StartProcess(name string, r *ActionResult) error

func (*API) StatusAll

func (a *API) StatusAll(name string, r *ProcessGroupStatus) error

func (*API) StatusGroup

func (a *API) StatusGroup(name string, r *ProcessGroupStatus) error

func (*API) StatusProcess

func (a *API) StatusProcess(name string, r *ProcessStatus) error

func (*API) StopAll

func (a *API) StopAll(unused interface{}, r *ActionResult) error

func (*API) StopGroup

func (a *API) StopGroup(name string, r *ActionResult) error

func (*API) StopProcess

func (a *API) StopProcess(name string, r *ActionResult) error

func (*API) Summary

func (a *API) Summary(unused interface{}, s *Summary) error

func (*API) UnmonitorAll

func (a *API) UnmonitorAll(unused interface{}, r *ActionResult) error

func (*API) UnmonitorGroup

func (a *API) UnmonitorGroup(name string, r *ActionResult) error

func (*API) UnmonitorProcess

func (a *API) UnmonitorProcess(name string, r *ActionResult) error

type About

type About struct {
	Version    string
	Id         string
	Incaration uint64
}

type Action

type Action struct {
	Name   string
	Events []string
}

type ActionError

type ActionError struct {
	Err error
}

wrap errors returned by API methods so client can disambiguate between API errors and rpc errors

func (*ActionError) Error

func (e *ActionError) Error() string

type ActionResult

type ActionResult struct {
	Total  int
	Errors int
}

type AlertMessage

type AlertMessage struct {
	Action      string    `json:"action"`
	Rule        string    `json:"rule"`
	Date        time.Time `json:"date"`
	Service     string    `json:"service"`
	Description string    `json:"description"`
	Value       float64   `json:"value"`
	Message_id  uint64    `json:"message_id"`
}

The JSON message that is sent in alerts.

type CliClient

type CliClient interface {
	Call(action string, name string) (interface{}, error)
	Close() error
}

API interface for cli commands. When gonit is running as a daemon, will be RPCs; Otherwise, invoke the API in-process via reflection.

func NewLocalClient

func NewLocalClient(rcvr interface{}) CliClient

func NewRemoteClient

func NewRemoteClient(client *rpc.Client, rcvr interface{}) CliClient

type CliFormatter

type CliFormatter interface {
	Print(w io.Writer)
}

format data returned from the API

type ConfigManager

type ConfigManager struct {
	ProcessGroups map[string]*ProcessGroup
	Settings      *Settings
	// contains filtered or unexported fields
}

func (*ConfigManager) AddProcess

func (c *ConfigManager) AddProcess(groupName string, process *Process) error

XXX TODO needed for tests, a form of this should probably be in ConfigManager

func (*ConfigManager) ApplyDefaultSettings

func (c *ConfigManager) ApplyDefaultSettings()

Applies default global settings if some options haven't been specified.

func (*ConfigManager) FindGroup

func (c *ConfigManager) FindGroup(name string) (*ProcessGroup, error)

TODO should probably be in configmanager.go Helper method to find a ProcessGroup by name

func (*ConfigManager) FindProcess

func (c *ConfigManager) FindProcess(name string) (*Process, error)

BUG(lisbakke): If there are two processes named the same thing in different process groups, this could return the wrong process. ConfigManager should enforce unique group/process names. XXX TODO should probably be in configmanager.go Helper methods to find a Process by name

func (*ConfigManager) LoadConfig

func (c *ConfigManager) LoadConfig(path string) error

Main function to call, parses a path for gonit config file(s).

func (*ConfigManager) VisitProcesses

func (c *ConfigManager) VisitProcesses(visit func(p *Process) bool)

XXX TODO should probably be in configmanager.go Visit each Process in the ConfigManager. Stop visiting if visit func returns false

type Control

type Control struct {
	ConfigManager *ConfigManager
	EventMonitor  EventMonitorInterface
	States        map[string]*ProcessState
	// contains filtered or unexported fields
}

func (*Control) Config

func (c *Control) Config() *ConfigManager

ConfigManager accessor (exported for tests)

func (*Control) DoAction

func (c *Control) DoAction(name string, action *ControlAction) error

Invoke given action for the given process and its dependents and/or dependencies

func (*Control) IsMonitoring

func (c *Control) IsMonitoring(process *Process) bool

func (*Control) LoadPersistState

func (c *Control) LoadPersistState() error

func (*Control) PersistStates

func (c *Control) PersistStates(states map[string]*ProcessState) error

func (*Control) RegisterEventMonitor

func (c *Control) RegisterEventMonitor(eventMonitor *EventMonitor)

Registers the event monitor with Control so that it can turn event monitoring on/off when processes are started/stopped.

func (*Control) State

func (c *Control) State(process *Process) *ProcessState

type ControlAction

type ControlAction struct {
	// contains filtered or unexported fields
}

func NewControlAction

func NewControlAction(method int) *ControlAction

func NewGroupControlAction

func NewGroupControlAction(method int) *ControlAction

type ControlInterface

type ControlInterface interface {
	DoAction(name string, action *ControlAction) error
	IsMonitoring(process *Process) bool
}

type DataTimestamp

type DataTimestamp struct {
	// contains filtered or unexported fields
}

type Event

type Event struct {
	Name        string
	Description string
	Rule        string
	Duration    string
	Interval    string
}

type EventMonitor

type EventMonitor struct {
	// contains filtered or unexported fields
}

Managers the monitoring of event rules. It gets the rules from the configmanager, parses them, sets up a timer, then begins monitoring their resource values from resourcemanager. If any events trigger, it will take appropriate action.

func (*EventMonitor) CleanDataForProcess

func (e *EventMonitor) CleanDataForProcess(p *Process)

func (*EventMonitor) IsMonitoring

func (e *EventMonitor) IsMonitoring(p *Process) bool

func (*EventMonitor) Start

func (e *EventMonitor) Start(configManager *ConfigManager,
	control *Control) error

Given a configmanager config, this function starts the eventmonitor on monitoring events and dispatching them.

func (*EventMonitor) StartMonitoringProcess

func (e *EventMonitor) StartMonitoringProcess(p *Process)

func (*EventMonitor) Stop

func (e *EventMonitor) Stop()

func (*EventMonitor) TriggerAlerts

func (e *EventMonitor) TriggerAlerts(p *Process) bool

func (*EventMonitor) TriggerProcessActions

func (e *EventMonitor) TriggerProcessActions(p *Process) bool

type EventMonitorInterface

type EventMonitorInterface interface {
	StartMonitoringProcess(process *Process)
	Start(configManager *ConfigManager, control *Control) error
	Stop()
}

So we can mock it in tests.

type LoggerConfig

type LoggerConfig struct {
	Level    string
	FileName string
	Codec    string
	// contains filtered or unexported fields
}

func (*LoggerConfig) Close

func (lc *LoggerConfig) Close() error

func (*LoggerConfig) Init

func (lc *LoggerConfig) Init() error

type ParsedEvent

type ParsedEvent struct {
	// contains filtered or unexported fields
}

After configmanager gets the rules to be monitored, eventmonitor parses the rules and stores their data as ParsedEvent.

type Process

type Process struct {
	Name        string
	Pidfile     string
	Start       string
	Stop        string
	Restart     string
	Gid         string
	Uid         string
	Stdout      string
	Stderr      string
	Env         []string
	Dir         string
	Description string
	DependsOn   []string
	Actions     map[string][]string
	MonitorMode string
}

func (*Process) Command

func (p *Process) Command(program string) (*exec.Cmd, error)

exec.Cmd wrapper

func (*Process) IsMonitoringModeActive

func (p *Process) IsMonitoringModeActive() bool

func (*Process) IsMonitoringModeManual

func (p *Process) IsMonitoringModeManual() bool

func (*Process) IsMonitoringModePassive

func (p *Process) IsMonitoringModePassive() bool

func (*Process) IsRunning

func (p *Process) IsRunning() bool

Helper method to check if process is running via Pidfile

func (*Process) Pid

func (p *Process) Pid() (int, error)

Read pid from Pidfile

func (*Process) Redirect

func (p *Process) Redirect(fd *io.Writer, where string) error

Redirect child process fd (stdout | stderr) to a file

func (*Process) RestartProcess

func (p *Process) RestartProcess() error

Restart a process: Spawn Restart program if configured, otherwise call StopProcess() + StartProcess()

func (*Process) SavePid

func (p *Process) SavePid(pid int) error

Write pid to Pidfile

func (*Process) Spawn

func (p *Process) Spawn(program string) (*exec.Cmd, error)

Fork+Exec program with std{out,err} redirected and new session so program becomes the session and process group leader.

func (*Process) StartProcess

func (p *Process) StartProcess() (int, error)

Start a process. Process must manage its own Pidfile.

func (*Process) StopProcess

func (p *Process) StopProcess() error

Stop a process: Spawn Stop program if configured, otherwise send SIGTERM.

type ProcessGroup

type ProcessGroup struct {
	Name      string
	Events    map[string]*Event
	Processes map[string]*Process
}

func (*ProcessGroup) EventByName

func (pg *ProcessGroup) EventByName(eventName string) *Event

Given an action string name, returns the events associated with it.

type ProcessGroupStatus

type ProcessGroupStatus struct {
	Name  string
	Group []ProcessStatus
}

func (*ProcessGroupStatus) Len

func (pgs *ProcessGroupStatus) Len() int

func (*ProcessGroupStatus) Less

func (pgs *ProcessGroupStatus) Less(i, j int) bool

func (*ProcessGroupStatus) Print

func (g *ProcessGroupStatus) Print(w io.Writer)

func (*ProcessGroupStatus) Swap

func (pgs *ProcessGroupStatus) Swap(i, j int)

type ProcessState

type ProcessState struct {
	Monitor     int
	MonitorLock sync.Mutex
	Starts      int
	// contains filtered or unexported fields
}

XXX TODO should state be attached to Process type?

type ProcessStatus

type ProcessStatus struct {
	Summary ProcessSummary
	Pid     int
	State   sigar.ProcState
	Time    sigar.ProcTime
	Mem     sigar.ProcMem
}

func (*ProcessStatus) Print

func (p *ProcessStatus) Print(w io.Writer)

type ProcessSummary

type ProcessSummary struct {
	Name         string
	Running      bool
	ControlState ProcessState
}

type ResourceHolder

type ResourceHolder struct {
	// contains filtered or unexported fields
}

type ResourceManager

type ResourceManager struct {
	// contains filtered or unexported fields
}

Don't create more.

func (*ResourceManager) CleanData

func (r *ResourceManager) CleanData()

Cleans data from ResourceManager.

func (*ResourceManager) CleanDataForProcess

func (r *ResourceManager) CleanDataForProcess(p *Process)

Cleans up the resource data used for a process's event monitors.

func (*ResourceManager) ClearCachedResources

func (r *ResourceManager) ClearCachedResources()

Clears the resources cache.

func (*ResourceManager) GetResource

func (r *ResourceManager) GetResource(parsedEvent *ParsedEvent,
	pid int) (uint64, error)

Takes a ParsedEvent and pid and returns the value for the resource used in the rule.

func (*ResourceManager) IsValidResourceName

func (r *ResourceManager) IsValidResourceName(resourceName string) bool

Checks to see if a resource is a valid resource.

func (*ResourceManager) ParseAmount

func (r *ResourceManager) ParseAmount(resourceName string,
	amount string) (uint64, error)

A utility function that parses a rule's amount string and returns the value as the correct type. For instance, in a rule such as 'memory_used > 14mb' the amount is '14mb' which this function would turn into a uint64 of 14*1024*1024.

func (*ResourceManager) SetSigarInterface

func (r *ResourceManager) SetSigarInterface(sigar SigarInterface)

Allows the sigar interface to be set so that tests can set it.

type RpcServer

type RpcServer struct {
	// contains filtered or unexported fields
}

func NewRpcServer

func NewRpcServer(listenURL string) (*RpcServer, error)

Construct a new RpcServer via string URL Currently supporting json RPC over unix or tcp socket

func (*RpcServer) Serve

func (s *RpcServer) Serve() error

Accept unix|tcp connections and serve json RPCs

func (*RpcServer) Shutdown

func (s *RpcServer) Shutdown()

Close listener socket In the case of unix socket, remove socket file.

type Settings

type Settings struct {
	AlertTransport      string
	SocketFile          string
	RpcServerUrl        string
	ProcessPollInterval int
	Daemon              *Process
	PersistFile         string
	Logging             *LoggerConfig
}

func (*Settings) ApplyDefaults

func (settings *Settings) ApplyDefaults()

type SigarGetter

type SigarGetter struct{}

type SigarInterface

type SigarInterface interface {
	// contains filtered or unexported methods
}

This interface allows us to mock sigar in unit tests.

type Summary

type Summary struct {
	Processes []ProcessSummary
}

func (*Summary) Len

func (s *Summary) Len() int

func (*Summary) Less

func (s *Summary) Less(i, j int) bool

func (*Summary) Print

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

func (*Summary) Swap

func (s *Summary) Swap(i, j int)

type SystemStatus

type SystemStatus struct {
}

type Watcher

type Watcher struct {
	Control *Control
	// contains filtered or unexported fields
}

func (*Watcher) Check

func (w *Watcher) Check()

func (*Watcher) Start

func (w *Watcher) Start()

func (*Watcher) Stop

func (w *Watcher) Stop()

Notes

Bugs

  • If there are two processes named the same thing in different process groups, this could return the wrong process. ConfigManager should enforce unique group/process names. XXX TODO should probably be in configmanager.go Helper methods to find a Process by name

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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