Added basic structure for checks
This commit is contained in:
		
							
								
								
									
										14
									
								
								check.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								check.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,14 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func NewCheck(name string, addr string) (AlrmCheck, error) {
 | 
				
			||||||
 | 
						switch name {
 | 
				
			||||||
 | 
						case "ping":
 | 
				
			||||||
 | 
							return &CheckPing{Address: addr}, nil
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("unknown check name \"%s\"", name)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										13
									
								
								check_ping.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								check_ping.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type CheckPing struct {
 | 
				
			||||||
 | 
						Address string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *CheckPing) Check() error {
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *CheckPing) Parse(tk string) (bool, error) {
 | 
				
			||||||
 | 
						return false, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										13
									
								
								config.go
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								config.go
									
									
									
									
									
								
							@ -25,6 +25,19 @@ func (ag *AlrmGroup) NewHost() *AlrmHost {
 | 
				
			|||||||
type AlrmHost struct {
 | 
					type AlrmHost struct {
 | 
				
			||||||
	Name    string
 | 
						Name    string
 | 
				
			||||||
	Address string
 | 
						Address string
 | 
				
			||||||
 | 
						Checks  []AlrmCheck
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (ah *AlrmHost) GetAddress() string {
 | 
				
			||||||
 | 
						if ah.Address != "" {
 | 
				
			||||||
 | 
							return ah.Address
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return ah.Name
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type AlrmCheck interface {
 | 
				
			||||||
 | 
						Parse(string) (bool, error)
 | 
				
			||||||
 | 
						Check() error
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func ReadConfig(fn string) (*AlrmConfig, error) {
 | 
					func ReadConfig(fn string) (*AlrmConfig, error) {
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										64
									
								
								parser.go
									
									
									
									
									
								
							
							
						
						
									
										64
									
								
								parser.go
									
									
									
									
									
								
							@ -14,6 +14,7 @@ const (
 | 
				
			|||||||
	TK_MONITOR
 | 
						TK_MONITOR
 | 
				
			||||||
	TK_GROUP
 | 
						TK_GROUP
 | 
				
			||||||
	TK_HOST
 | 
						TK_HOST
 | 
				
			||||||
 | 
						TK_CHECK
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Parser struct {
 | 
					type Parser struct {
 | 
				
			||||||
@ -31,6 +32,7 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) {
 | 
				
			|||||||
	config := &AlrmConfig{}
 | 
						config := &AlrmConfig{}
 | 
				
			||||||
	var group *AlrmGroup
 | 
						var group *AlrmGroup
 | 
				
			||||||
	var host *AlrmHost
 | 
						var host *AlrmHost
 | 
				
			||||||
 | 
						var check AlrmCheck
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	scan := bufio.NewScanner(file)
 | 
						scan := bufio.NewScanner(file)
 | 
				
			||||||
	scan.Split(p.Split)
 | 
						scan.Split(p.Split)
 | 
				
			||||||
@ -51,22 +53,25 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		case TK_SET:
 | 
							case TK_SET:
 | 
				
			||||||
			key := strings.ToLower(tk)
 | 
								key := strings.ToLower(tk)
 | 
				
			||||||
			if scan.Scan() {
 | 
								if !scan.Scan() {
 | 
				
			||||||
				value := scan.Text()
 | 
									return nil, fmt.Errorf("empty value name for set in %s, line %d",
 | 
				
			||||||
				switch key {
 | 
										fn, p.Line+1)
 | 
				
			||||||
				case "interval":
 | 
								}
 | 
				
			||||||
					config.Interval, err = strconv.Atoi(value)
 | 
					
 | 
				
			||||||
					if err != nil {
 | 
								value := scan.Text()
 | 
				
			||||||
						return nil, fmt.Errorf(
 | 
								switch key {
 | 
				
			||||||
							"invalid number for interval, line %d: \"%s\"",
 | 
								case "interval":
 | 
				
			||||||
							p.Line+1, value,
 | 
									config.Interval, err = strconv.Atoi(value)
 | 
				
			||||||
						)
 | 
									if err != nil {
 | 
				
			||||||
					}
 | 
										return nil, fmt.Errorf(
 | 
				
			||||||
				default:
 | 
											"invalid number for interval in %s, line %d: \"%s\"",
 | 
				
			||||||
					return nil, fmt.Errorf("invalid key for set, line %d: \"%s\"",
 | 
											fn, p.Line+1, value,
 | 
				
			||||||
						p.Line+1, tk,
 | 
					 | 
				
			||||||
					)
 | 
										)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
								default:
 | 
				
			||||||
 | 
									return nil, fmt.Errorf("unknown key for set in %s, line %d: \"%s\"",
 | 
				
			||||||
 | 
										fn, p.Line+1, tk,
 | 
				
			||||||
 | 
									)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			p.prevState()
 | 
								p.prevState()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -87,7 +92,7 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		case TK_GROUP:
 | 
							case TK_GROUP:
 | 
				
			||||||
			if group == nil {
 | 
								if group == nil {
 | 
				
			||||||
				return nil, fmt.Errorf("Group token without initialization")
 | 
									return nil, fmt.Errorf("group without initialization")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			switch strings.ToLower(tk) {
 | 
								switch strings.ToLower(tk) {
 | 
				
			||||||
@ -108,7 +113,7 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		case TK_HOST:
 | 
							case TK_HOST:
 | 
				
			||||||
			if host == nil {
 | 
								if host == nil {
 | 
				
			||||||
				return nil, fmt.Errorf("Host token without initialization")
 | 
									return nil, fmt.Errorf("host token without initialization")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if host.Name == "" {
 | 
								if host.Name == "" {
 | 
				
			||||||
@ -118,15 +123,36 @@ func (p *Parser) Parse(fn string) (*AlrmConfig, error) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			switch strings.ToLower(tk) {
 | 
								switch strings.ToLower(tk) {
 | 
				
			||||||
			case "address":
 | 
								case "address":
 | 
				
			||||||
				if scan.Scan() {
 | 
									if !scan.Scan() {
 | 
				
			||||||
					host.Address = scan.Text()
 | 
										return nil, fmt.Errorf("empty address for host in %s, line %d",
 | 
				
			||||||
 | 
											fn, p.Line+1)
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
									host.Address = scan.Text()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case "check":
 | 
				
			||||||
 | 
									check = nil
 | 
				
			||||||
 | 
									p.setState(TK_CHECK)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			default:
 | 
								default:
 | 
				
			||||||
				p.prevState()
 | 
									p.prevState()
 | 
				
			||||||
				goto stateswitch
 | 
									goto stateswitch
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							case TK_CHECK:
 | 
				
			||||||
 | 
								if check == nil {
 | 
				
			||||||
 | 
									if host == nil {
 | 
				
			||||||
 | 
										return nil, fmt.Errorf("host token without initialization")
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									check, err = NewCheck(strings.ToLower(tk), host.GetAddress())
 | 
				
			||||||
 | 
									if err != nil {
 | 
				
			||||||
 | 
										return nil, fmt.Errorf("%s in %s, line %d",
 | 
				
			||||||
 | 
											err.Error(), fn, p.Line+1)
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									host.Checks = append(host.Checks, check)
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								check.Parse(tk)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
			return nil, fmt.Errorf("unknown parser state: %d", p.state())
 | 
								return nil, fmt.Errorf("unknown parser state: %d", p.state())
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@ -169,6 +195,8 @@ func (p *Parser) stateName() string {
 | 
				
			|||||||
		return "TK_GROUP"
 | 
							return "TK_GROUP"
 | 
				
			||||||
	case TK_HOST:
 | 
						case TK_HOST:
 | 
				
			||||||
		return "TK_HOST"
 | 
							return "TK_HOST"
 | 
				
			||||||
 | 
						case TK_CHECK:
 | 
				
			||||||
 | 
							return "TK_CHECK"
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
		return "UNKNOWN"
 | 
							return "UNKNOWN"
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -8,5 +8,6 @@ monitor host stupid.com
 | 
				
			|||||||
monitor group webservers
 | 
					monitor group webservers
 | 
				
			||||||
  # Monitor host one
 | 
					  # Monitor host one
 | 
				
			||||||
  host one.com address 10.12.121.1
 | 
					  host one.com address 10.12.121.1
 | 
				
			||||||
 | 
							check ping
 | 
				
			||||||
  # Monitor host two
 | 
					  # Monitor host two
 | 
				
			||||||
  host two.com
 | 
					  host two.com
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user