2021-03-13 03:17:30 +04:00
|
|
|
/*
|
|
|
|
httpprocwatchd provides HTTP interface to a list of process' statuses
|
|
|
|
formatted as JSON.
|
|
|
|
Copyright (c) 2021 Alexander "Arav" Andreev <me@arav.top>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2022-01-02 02:56:46 +04:00
|
|
|
. "httpprocwatchd/prog"
|
2021-03-13 03:17:30 +04:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2021-12-24 23:28:50 +04:00
|
|
|
// listWatchedProcesses prints a list of processes being watched.
|
|
|
|
func listWatchedProcesses(processes *[]string) {
|
|
|
|
for _, v := range *processes {
|
|
|
|
fmt.Printf("%s, ", v)
|
2021-11-03 23:52:57 +04:00
|
|
|
}
|
2021-12-24 23:28:50 +04:00
|
|
|
fmt.Println()
|
2021-11-03 23:52:57 +04:00
|
|
|
}
|
|
|
|
|
2021-12-24 23:28:50 +04:00
|
|
|
// version prints information about program.
|
2021-03-13 03:17:30 +04:00
|
|
|
func version() {
|
2021-12-24 23:28:50 +04:00
|
|
|
fmt.Println("httpprocwatchd ver. 1.2")
|
2021-03-13 03:17:30 +04:00
|
|
|
fmt.Println("Copyright (c) 2021 Alexander \"Arav\" Andreev <me@arav.top>")
|
2021-11-03 23:52:57 +04:00
|
|
|
fmt.Println("License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.")
|
2021-11-04 00:17:17 +04:00
|
|
|
fmt.Println("This is free software, and you are welcome to change and redistribute it.")
|
|
|
|
fmt.Println("There is NO WARRANTY, to the extent permitted by law.")
|
2021-03-13 03:17:30 +04:00
|
|
|
}
|
|
|
|
|
2021-12-24 23:28:50 +04:00
|
|
|
func main() {
|
|
|
|
var oConfigPath string
|
|
|
|
var oShowVersion bool
|
|
|
|
var oListProcesses bool
|
2021-11-03 23:52:57 +04:00
|
|
|
|
2021-12-24 23:28:50 +04:00
|
|
|
var oAddProcess string
|
|
|
|
var oRemoveProcess string
|
2021-11-03 23:52:57 +04:00
|
|
|
|
2021-03-13 03:17:30 +04:00
|
|
|
log.SetFlags(0)
|
|
|
|
|
2022-01-02 02:56:46 +04:00
|
|
|
flag.StringVar(&oConfigPath, "config", "config.json", "path to configuration file")
|
|
|
|
flag.StringVar(&oConfigPath, "c", "config.json", "path to configuration file (shorthand)")
|
2021-11-03 23:52:57 +04:00
|
|
|
flag.BoolVar(&oShowVersion, "version", false, "show version")
|
|
|
|
flag.BoolVar(&oShowVersion, "v", false, "show version (shorthand)")
|
|
|
|
|
|
|
|
flag.BoolVar(&oListProcesses, "list", false, "list watched processes")
|
|
|
|
flag.BoolVar(&oListProcesses, "l", false, "list watched processes (shorthand)")
|
|
|
|
|
|
|
|
flag.StringVar(&oAddProcess, "add", "", "add process to list")
|
|
|
|
flag.StringVar(&oAddProcess, "a", "", "add process to list (shorthand)")
|
|
|
|
|
|
|
|
flag.StringVar(&oRemoveProcess, "remove", "", "remove process from list")
|
|
|
|
flag.StringVar(&oRemoveProcess, "r", "", "remove process from list (shorthand)")
|
2021-03-13 03:17:30 +04:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2021-11-03 23:52:57 +04:00
|
|
|
if oShowVersion {
|
2021-03-13 03:17:30 +04:00
|
|
|
version()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := exec.LookPath("pgrep"); err != nil {
|
|
|
|
log.Fatalln(ErrPgrepNotFound)
|
|
|
|
}
|
|
|
|
|
2021-11-03 23:52:57 +04:00
|
|
|
conf, err := LoadConfiguration(oConfigPath)
|
2021-03-13 03:17:30 +04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Cannot load configuration file: %s\n", err)
|
|
|
|
}
|
|
|
|
|
2021-11-03 23:52:57 +04:00
|
|
|
if oListProcesses {
|
2021-12-24 23:28:50 +04:00
|
|
|
listWatchedProcesses(&conf.Processes)
|
2021-11-03 23:52:57 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if oAddProcess != "" {
|
|
|
|
err := AddProcessToList(oAddProcess, conf, oConfigPath)
|
|
|
|
if err != nil {
|
2021-11-04 00:17:17 +04:00
|
|
|
log.Fatalf("Cannot add process: %s\n", err)
|
2021-11-03 23:52:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if oRemoveProcess != "" {
|
|
|
|
err := RemoveProcessFromList(oRemoveProcess, conf, oConfigPath)
|
|
|
|
if err != nil {
|
2021-11-04 00:17:17 +04:00
|
|
|
log.Fatalf("Cannot remove process: %s\n", err)
|
2021-11-03 23:52:57 +04:00
|
|
|
}
|
|
|
|
}
|
2021-03-13 03:17:30 +04:00
|
|
|
|
2021-12-24 23:28:50 +04:00
|
|
|
// If we modified a list then let's look for a running program and
|
|
|
|
// send SIGHUP for it to reload a list.
|
|
|
|
if oAddProcess != "" || oRemoveProcess != "" {
|
|
|
|
isup, pid, _ := IsProcessUp("httpprocwatchd")
|
|
|
|
if isup && pid != nil && len(pid) > 1 {
|
|
|
|
var trgt_pid int
|
|
|
|
if pid[0] == os.Getpid() {
|
|
|
|
trgt_pid = pid[1]
|
|
|
|
} else {
|
|
|
|
trgt_pid = pid[0]
|
|
|
|
}
|
|
|
|
proc, err := os.FindProcess(trgt_pid)
|
|
|
|
if err == nil {
|
|
|
|
proc.Signal(syscall.SIGHUP)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2021-03-13 03:17:30 +04:00
|
|
|
}
|
|
|
|
|
2022-01-02 02:56:46 +04:00
|
|
|
srv := CreateAndStartHTTPServer(conf)
|
2021-03-13 03:17:30 +04:00
|
|
|
|
2021-12-24 23:28:50 +04:00
|
|
|
syssignal := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(syssignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
2021-03-13 03:17:30 +04:00
|
|
|
|
|
|
|
log.Printf("httpprocwatchd is running on \"%s\".", conf.ListenAddress)
|
|
|
|
|
2021-12-24 23:28:50 +04:00
|
|
|
for {
|
|
|
|
switch <-syssignal {
|
|
|
|
case os.Interrupt:
|
|
|
|
fallthrough
|
|
|
|
case syscall.SIGINT | syscall.SIGTERM:
|
|
|
|
log.Println("Shutting down... ")
|
2022-01-02 02:56:46 +04:00
|
|
|
ShutdownHTTPServer(srv)
|
2021-12-24 23:28:50 +04:00
|
|
|
log.Println("Server shutted down.")
|
|
|
|
os.Exit(0)
|
|
|
|
case syscall.SIGHUP:
|
|
|
|
newconf, err := LoadConfiguration(oConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to reload configuration: %s\n", err)
|
|
|
|
}
|
|
|
|
conf.Processes = newconf.Processes
|
|
|
|
log.Println("Successfully reloaded a list of watched processes.")
|
|
|
|
}
|
2021-03-13 03:17:30 +04:00
|
|
|
}
|
|
|
|
}
|