예제 #1
0
//Telegram sends a Telegram message to one or more users by their unique ID
func Telegram(targets []string, message string) {
	var err error

	//Check if a bot instance have been constructed before
	if bot == nil {
		setupTelegram()
	}

	if err == nil {

		if len(targets) >= 1 {
			//If targets have been set up in the specific service, use them
			for _, target := range targets {
				//disable_web_page_preview bool, parse_mode string
				result, errMsg := bot.SendMessage(target, message, true, "Markdown")
				if errMsg != nil {
					log.Error("Error sending Telegram message to", target, "containing", message)
				}
				checkMessageError(result, err)
			}
		} else {
			//If not, then use default one
			target := configuration.C.Actions.Telegram.Target
			result, errMsg := bot.SendMessage(target, message, true, "Markdown")
			if errMsg != nil {
				log.Error("Error sending Telegram message to", target, "containing", message)
			}
			checkMessageError(result, err)
		}

	} else {
		log.Error("Error sending Telegram message!", err.Error())
	}
}
예제 #2
0
func checkMessageError(result golegram.Message, err error) {
	if err != nil {
		log.Error("[Telegram] Message error")
		log.Error(err)
		log.Error(result)
	}
}
예제 #3
0
func setupTelegram() {
	var err error
	log.Error("Instance bot initialized! Using token:", configuration.C.Actions.Telegram.Bot)
	bot, err = golegram.NewBot(configuration.C.Actions.Telegram.Bot)
	if err != nil {
		log.Error("[Telegram] Init error")
		log.Error(err)
	}
}
예제 #4
0
//Init ializes the configuration
func Init(configfile string) {
	var configContent []byte
	var tempContent []byte
	var err error

	tempContent, err = ioutil.ReadFile(configfile)
	if err == nil {
		configContent = tempContent
	} else {
		tempContent, err = ioutil.ReadFile("config.yaml")
		if err == nil {
			configContent = tempContent
		} else {
			panic("Not found in folder! Panic!")
		}
	}

	m := Configuration{}
	err = yaml.Unmarshal(configContent, &m)

	errUnmarshal := yaml.Unmarshal(configContent, &C)

	if errUnmarshal != nil {
		log.Error("Cannot load configuration! Make sure the configuration file matches your version of monitoring.")
		panic(errUnmarshal.Error())
	}

	name, err := os.Hostname()
	if err != nil {
		panic(err)
	} else {
		C.Hostname = name
	}

	if len(C.Actions.Telegram.Target) > 2 {
		log.Notice("Telegram enabled.")
		C.Actions.Telegram.Enabled = true
	} else {
		log.Error("Telegram is disabled.")
	}

	IsLoaded = true
	log.Notice("Services: ", C.Paths.Services)
	log.Notice("Checks: ", C.Paths.Checks)
}
예제 #5
0
//Spawn starts a checkDispatcher
func Spawn() {
	if !spawned {
		log.Warning("Spawning the daemon!")
		services.Load()
		go checkDispatcher()
	} else {
		log.Error("Daemon is already active!")
	}
}
예제 #6
0
func Update(c *gin.Context) {
	identifier := c.Param("identifier")
	var result string
	if identifier != "" {
		service, _ := services.GetService(identifier)

		err := services.UpdateService(service)
		if err != nil {
			result = "Could not update service."
			log.Error(result, identifier)
		}

	} else {
		result = "No parameter to update!"
	}
	c.JSON(200, gin.H{
		"result": result,
	})
}
예제 #7
0
파일: child.go 프로젝트: mdeheij/monitoring
//Execute a command by specifying executable and arguments, returns statuscode and output summary
func Execute(timeout int, cmdName string, cmdArgs ...string) (status int, output string) {
	// TODO: Check if cmdName file exists
	cmd := exec.Command(cmdName, cmdArgs...)

	cmdOutput := &bytes.Buffer{}
	errOutput := &bytes.Buffer{}

	fail := false
	killed := false

	cmd.Stdout = cmdOutput
	cmd.Stderr = errOutput

	var waitStatus syscall.WaitStatus

	cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
	cmd.Start()

	timer := time.AfterFunc(time.Duration(timeout+1)*time.Second, func() {
		if cmd.Process != nil {
			pgid, err := syscall.Getpgid(cmd.Process.Pid)
			if err == nil {
				syscall.Kill(-pgid, 9) // note the minus sign
				log.Notice("Killed PID", pgid, "because of timeout of ", timeout, "seconds while running", cmdName)
				killed = true
			}
		} else {
			log.Error("Tried to kill but it didn't exist (anymore).", cmdName, cmdArgs)
		}
	})

	if err := cmd.Wait(); err != nil {
		if err != nil {
			fail = true
		}
		if exitError, ok := err.(*exec.ExitError); ok {
			waitStatus = exitError.Sys().(syscall.WaitStatus)
		}
	} else {
		// Success
		waitStatus = cmd.ProcessState.Sys().(syscall.WaitStatus)
	}

	if killed {
		return killedStatus()
	}

	timer.Stop()

	outputString := string(cmdOutput.Bytes())
	shortOutputStrings := strings.Split(outputString, "\n")
	statusCode := waitStatus.ExitStatus()

	if waitStatus.ExitStatus() == -1 {
		return killedStatus()
	}

	if waitStatus.ExitStatus() == 0 && fail {
		statusCode = 420
	}

	return statusCode, shortOutputStrings[0]
}