func checkCertExpiration() *checkers.Checker {
	opts := certOpts{}
	psr := flags.NewParser(&opts, flags.Default)
	_, err := psr.Parse()
	if err != nil {
		psr.WriteHelp(os.Stdout)
		os.Exit(1)
	}

	cfByte, err := ioutil.ReadFile(opts.CertFile)
	if err != nil {
		return checkers.Critical(err.Error())
	}
	cfBlock, _ := pem.Decode(cfByte)
	cfCrt, err := x509.ParseCertificate(cfBlock.Bytes)

	if err != nil {
		return checkers.Critical(err.Error())
	}

	cfDaysRemaining := int64(cfCrt.NotAfter.Sub(time.Now().UTC()).Hours() / 24)
	checkSt := checkers.OK
	msg := fmt.Sprintf("%d days remaining", cfDaysRemaining)

	if cfDaysRemaining < opts.Crit {
		checkSt = checkers.CRITICAL
	} else if cfDaysRemaining < opts.Warn {
		checkSt = checkers.WARNING
	}
	return checkers.NewChecker(checkSt, msg)
}
Example #2
0
func (opts *sshOpts) run() *checkers.Checker {
	// prevent changing output of some commands
	os.Setenv("LANG", "C")
	os.Setenv("LC_ALL", "C")

	config, err := opts.makeClientConfig()
	if err != nil {
		return checkers.Unknown(err.Error())
	}

	start := time.Now()
	client, err := opts.dial(config)
	if err != nil {
		if addrerr, ok := err.(*net.AddrError); ok {
			if addrerr.Timeout() {
				elapsed := time.Now().Sub(start)
				return opts.checkTimeoutError(elapsed, err)
			} else if addrerr.Temporary() {
				return checkers.Warning(err.Error())
			}
		}
		return checkers.Critical(err.Error())
	}
	session, err := client.NewSession()
	if err != nil {
		return checkers.Critical(err.Error())
	}
	err = session.Close()
	if err != nil {
		return checkers.Unknown(err.Error())
	}
	elapsed := time.Now().Sub(start)
	return opts.checkTimeout(elapsed)
}
func checkUptime() *checkers.Checker {
	opts := connectionOpts{}
	psr := flags.NewParser(&opts, flags.Default)
	_, err := psr.Parse()
	if err != nil {
		os.Exit(1)
	}

	db := mysql.New("tcp", "", fmt.Sprintf("%s:%s", opts.mysqlSetting.Host, opts.mysqlSetting.Port), opts.mysqlSetting.User, opts.mysqlSetting.Pass, "")
	err = db.Connect()
	if err != nil {
		return checkers.Critical("couldn't connect DB")
	}
	defer db.Close()

	rows, res, err := db.Query("SHOW GLOBAL STATUS LIKE 'Uptime'")
	if err != nil {
		return checkers.Critical("couldn't execute query")
	}

	idxValue := res.Map("Value")
	Uptime := rows[0].Int64(idxValue)

	if opts.Crit > 0 && Uptime < opts.Crit {
		return checkers.Critical(fmt.Sprintf("up %s < %s", uptime2str(Uptime), uptime2str(opts.Crit)))
	} else if opts.Warn > 0 && Uptime < opts.Warn {
		return checkers.Warning(fmt.Sprintf("up %s < %s", uptime2str(Uptime), uptime2str(opts.Warn)))
	}
	return checkers.Ok(fmt.Sprintf("up %s", uptime2str(Uptime)))
}
func run(args []string) *checkers.Checker {
	_, err := flags.ParseArgs(&opts, args)
	if err != nil {
		return checkers.Critical(err.Error())
	}

	offset, err := getNtpOffset()
	if err != nil {
		return checkers.Unknown(err.Error())
	}

	var chkSt checkers.Status
	var msg string
	if opts.Crit < math.Abs(offset) {
		msg = fmt.Sprintf("ntp offset is over %f(actual) > %f(threshold)", math.Abs(offset), opts.Crit)
		chkSt = checkers.CRITICAL
	} else if opts.Warn < math.Abs(offset) {
		msg = fmt.Sprintf("ntp offset is over %f(actual) > %f(threshold)", math.Abs(offset), opts.Warn)
		chkSt = checkers.WARNING
	} else {
		msg = fmt.Sprintf("ntp offset is %f(actual) < %f(warning threshold), %f(critial threshold)", offset, opts.Warn, opts.Crit)
		chkSt = checkers.OK
	}

	return checkers.NewChecker(chkSt, msg)
}
Example #5
0
func (opts *options) run() *checkers.Checker {
	missingInstances, missingMembers, err := check(opts)
	if err != nil {
		return checkers.Unknown(err.Error())
	}

	if len(missingInstances) != 0 {
		var ipAddrs []string
		for _, instance := range missingInstances {
			ipAddrs = append(ipAddrs, aws.StringValue(instance.PrivateIpAddress))
		}

		return checkers.Critical(fmt.Sprintf("%d instance(s) left from Consul cluster: %v", len(ipAddrs), ipAddrs))
	}

	if len(missingMembers) != 0 {
		var ipAddrs []string
		for _, member := range missingMembers {
			ipAddrs = append(ipAddrs, fmt.Sprintf("%s(%s)", member.Node, member.Address))
		}

		return checkers.Warning(fmt.Sprintf("%d instance(s) not properly tagged: %v", len(ipAddrs), ipAddrs))
	}

	return checkers.Ok("OK")
}
Example #6
0
func checkSlave(args []string) *checkers.Checker {
	opts := redisSetting{}
	psr := flags.NewParser(&opts, flags.Default)
	psr.Usage = "slave [OPTIONS]"
	_, err := psr.ParseArgs(args)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	c, info, err := connectRedisGetInfo(opts)
	if err != nil {
		return checkers.Unknown(err.Error())
	}
	defer c.Close()

	if status, ok := (*info)["master_link_status"]; ok {
		msg := fmt.Sprintf("master_link_status: %s", status)

		switch status {
		case "up":
			return checkers.Ok(msg)
		case "down":
			return checkers.Critical(msg)
		default:
			return checkers.Unknown(msg)
		}

	} else {
		// it may be a master!
		return checkers.Unknown("couldn't get master_link_status")
	}
}
Example #7
0
func run(args []string) *checkers.Checker {
	_, err := flags.ParseArgs(&opts, args)
	if err != nil {
		os.Exit(1)
	}

	stTime := time.Now()
	resp, err := http.Get(opts.URL)
	if err != nil {
		return checkers.Critical(err.Error())
	}
	elapsed := time.Since(stTime)
	defer resp.Body.Close()

	cLength := resp.ContentLength
	if cLength == -1 {
		byt, _ := ioutil.ReadAll(resp.Body)
		cLength = int64(len(byt))
	}

	checkSt := checkers.UNKNOWN
	switch st := resp.StatusCode; true {
	case st < 400:
		checkSt = checkers.OK
	case st < 500:
		checkSt = checkers.WARNING
	default:
		checkSt = checkers.CRITICAL
	}

	msg := fmt.Sprintf("%s %s - %d bytes in %f second respons time",
		resp.Proto, resp.Status, cLength, elapsed.Seconds())

	return checkers.NewChecker(checkSt, msg)
}
func run(args []string) *checkers.Checker {
	opts, err := parseArgs(args)
	if err != nil {
		os.Exit(1)
	}

	client := &http.Client{Timeout: time.Duration(opts.Timeout) * time.Second}
	res, err := client.Get(createURL(opts))
	if err != nil {
		return checkers.Critical(err.Error())
	}

	defer res.Body.Close()
	if res.StatusCode != http.StatusOK {
		return checkers.Unknown(fmt.Sprintf("failed: http status code %d", res.StatusCode))
	}

	resJ := jmxJolokiaResponse{}
	dec := json.NewDecoder(res.Body)
	if err := dec.Decode(&resJ); err != nil {
		return checkers.Critical(err.Error())
	}

	if resJ.Status != 200 {
		return checkers.Unknown(fmt.Sprintf("failed: response status %d", resJ.Status))
	}

	checkSt := checkers.OK
	msg := fmt.Sprintf("%s %s value %f", opts.MBean, opts.Attribute, resJ.Value)
	if resJ.Value > opts.Critical {
		checkSt = checkers.CRITICAL
		msg = fmt.Sprintf("%s %s value is over %f > %f", opts.MBean, opts.Attribute, resJ.Value, opts.Critical)
	} else if resJ.Value > opts.Warning {
		checkSt = checkers.WARNING
		msg = fmt.Sprintf("%s %s value is over %f > %f", opts.MBean, opts.Attribute, resJ.Value, opts.Warning)
	}

	return checkers.NewChecker(checkSt, msg)
}
func run(args []string) *checkers.Checker {
	_, err := flags.ParseArgs(&opts, args)
	if err != nil {
		os.Exit(1)
	}

	mc := memcache.New(opts.Host + ":" + opts.Port)
	mc.Timeout = time.Duration(opts.Timeout) * time.Second

	err = mc.Set(&memcache.Item{Key: opts.Key, Value: []byte("Check key"), Expiration: 240})
	if err != nil {
		return checkers.Critical("couldn't set a key: " + err.Error())
	}

	item, err := mc.Get(opts.Key)
	if err != nil {
		return checkers.Critical("couldn't get a key: " + err.Error())
	}
	if string(item.Value) != "Check key" {
		return checkers.Critical("not correct value")
	}
	return checkers.Ok("Get,Set OK")
}
Example #10
0
func checkReplication(args []string) *checkers.Checker {
	opts := replicationOpts{}
	psr := flags.NewParser(&opts, flags.Default)
	psr.Usage = "replication [OPTIONS]"
	_, err := psr.ParseArgs(args)
	if err != nil {
		os.Exit(1)
	}
	db := newMySQL(opts.mysqlSetting)
	err = db.Connect()
	if err != nil {
		return checkers.Unknown("couldn't connect DB")
	}
	defer db.Close()

	rows, res, err := db.Query("SHOW SLAVE STATUS")
	if err != nil {
		return checkers.Unknown("couldn't execute query")
	}

	if len(rows) == 0 {
		return checkers.Ok("MySQL is not slave")
	}

	idxIoThreadRunning := res.Map("Slave_IO_Running")
	idxSQLThreadRunning := res.Map("Slave_SQL_Running")
	idxSecondsBehindMaster := res.Map("Seconds_Behind_Master")
	ioThreadStatus := rows[0].Str(idxIoThreadRunning)
	sqlThreadStatus := rows[0].Str(idxSQLThreadRunning)
	secondsBehindMaster := rows[0].Int64(idxSecondsBehindMaster)

	if !(ioThreadStatus == "Yes" && sqlThreadStatus == "Yes") {
		return checkers.Critical("MySQL replication has been stopped")
	}

	checkSt := checkers.OK
	msg := fmt.Sprintf("MySQL replication behind master %d seconds", secondsBehindMaster)
	if secondsBehindMaster > opts.Crit {
		checkSt = checkers.CRITICAL
	} else if secondsBehindMaster > opts.Warn {
		checkSt = checkers.WARNING
	}
	return checkers.NewChecker(checkSt, msg)
}
func run(args []string) *checkers.Checker {
	_, err := flags.ParseArgs(&opts, args)
	if err != nil {
		os.Exit(1)
	}

	client := &http.Client{}
	url := fmt.Sprintf("%s://%s:%d/_cluster/health", opts.Scheme, opts.Host, opts.Port)

	stTime := time.Now()
	resp, err := client.Get(url)
	if err != nil {
		return checkers.Critical(err.Error())
	}
	elapsed := time.Since(stTime)
	defer resp.Body.Close()

	dec := json.NewDecoder(resp.Body)
	var health healthStat
	dec.Decode(&health)

	checkSt := checkers.UNKNOWN
	switch health.Status {
	case "green":
		checkSt = checkers.OK
	case "yellow":
		checkSt = checkers.WARNING
	case "red":
		checkSt = checkers.CRITICAL
	default:
		checkSt = checkers.UNKNOWN
	}

	msg := fmt.Sprintf("%s (cluster: %s) - %f second respons time",
		health.Status, health.ClusterName, elapsed.Seconds())

	return checkers.NewChecker(checkSt, msg)
}
Example #12
0
func (opts *tcpOpts) run() *checkers.Checker {
	err := opts.prepare()
	if err != nil {
		return checkers.Unknown(err.Error())
	}
	// prevent changing output of some commands
	os.Setenv("LANG", "C")
	os.Setenv("LC_ALL", "C")

	address := fmt.Sprintf("%s:%d", opts.Hostname, opts.Port)
	start := time.Now()
	if opts.Delay > 0 {
		time.Sleep(time.Duration(opts.Delay) * time.Second)
	}
	var conn net.Conn
	if opts.UnixSock != "" {
		conn, err = dial("unix", opts.UnixSock, opts.SSL, opts.NoCheckCertificate)
	} else {
		conn, err = dial("tcp", address, opts.SSL, opts.NoCheckCertificate)
	}
	if err != nil {
		return checkers.Critical(err.Error())
	}
	defer conn.Close()

	if opts.Send != "" {
		err := write(conn, []byte(opts.Send), opts.Timeout)
		if err != nil {
			return checkers.Critical(err.Error())
		}
	}

	res := ""
	if opts.expectReg != nil {
		buf, err := slurp(conn, opts.MaxBytes, opts.Timeout)
		if err != nil {
			return checkers.Critical(err.Error())
		}
		res = string(buf)
		if !opts.expectReg.MatchString(res) {
			return checkers.Critical("Unexpected response from host/socket: " + res)
		}
	}

	if opts.Quit != "" {
		err := write(conn, []byte(opts.Quit), opts.Timeout)
		if err != nil {
			return checkers.Critical(err.Error())
		}
	}
	elapsed := time.Now().Sub(start)

	chkSt := checkers.OK
	if opts.Warning > 0 && elapsed > time.Duration(opts.Warning)*time.Second {
		chkSt = checkers.WARNING
	}
	if opts.Critical > 0 && elapsed > time.Duration(opts.Critical)*time.Second {
		chkSt = checkers.CRITICAL
	}
	msg := fmt.Sprintf("%.3f seconds response time on", float64(elapsed)/float64(time.Second))
	if opts.Hostname != "" {
		msg += " " + opts.Hostname
	}
	if opts.Port > 0 {
		msg += fmt.Sprintf(" port %d", opts.Port)
	}
	if res != "" {
		msg += fmt.Sprintf(" [%s]", strings.Trim(res, "\r\n"))
	}
	return checkers.NewChecker(chkSt, msg)
}