2021-03-13 03:17:30 +04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-12-15 04:00:26 +04:00
|
|
|
"context"
|
2023-12-15 04:02:56 +04:00
|
|
|
"encoding/json"
|
2021-03-13 03:17:30 +04:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2023-12-15 04:00:26 +04:00
|
|
|
"net/http"
|
2021-03-13 03:17:30 +04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2023-12-15 04:00:26 +04:00
|
|
|
"time"
|
2021-03-13 03:17:30 +04:00
|
|
|
)
|
|
|
|
|
2023-12-15 04:00:26 +04:00
|
|
|
var configPath *string = flag.String("c", "config.json", "path to configuration file")
|
2022-10-10 00:18:17 +04:00
|
|
|
var showVersion *bool = flag.Bool("v", false, "show version")
|
2021-11-03 23:52:57 +04:00
|
|
|
|
2023-12-15 04:00:26 +04:00
|
|
|
var version string
|
2021-11-03 23:52:57 +04:00
|
|
|
|
2024-01-13 03:54:34 +04:00
|
|
|
// Process contains an alias that will be returned when queried, and a process
|
|
|
|
// name to look for.
|
|
|
|
// A process is effectively a substring that is being looked in a cmdline file
|
|
|
|
// in /proc/ dir on unix-like systems.
|
|
|
|
type Process struct {
|
|
|
|
Process string `json:"process"`
|
2024-07-07 17:23:29 +04:00
|
|
|
Alias string `json:"alias"`
|
2024-01-13 03:54:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessesState is a map of processes' aliases and its statuses.
|
|
|
|
type ProcessesState map[string]bool
|
|
|
|
|
|
|
|
func GetProcessesState(procs *[]Process) (ps ProcessesState) {
|
2024-07-07 17:23:29 +04:00
|
|
|
ps = make(ProcessesState, len(*procs))
|
2024-01-13 03:54:34 +04:00
|
|
|
|
|
|
|
for _, proc := range *procs {
|
2024-07-07 17:23:29 +04:00
|
|
|
run, _ := IsProcessRuns(proc.Process)
|
|
|
|
ps[proc.Alias] = run
|
2024-01-13 03:54:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-13 04:42:19 +04:00
|
|
|
type Configuration struct {
|
|
|
|
ListenAddress string `json:"listen-address"`
|
|
|
|
Processes []Process `json:"processes"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func LoadConfiguration(path string) (conf *Configuration, err error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
conf = &Configuration{}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(f).Decode(conf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(conf.Processes); i++ {
|
|
|
|
if conf.Processes[i].Process == "" {
|
|
|
|
return nil, fmt.Errorf("an empty process field found")
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Processes[i].Alias == "" {
|
|
|
|
conf.Processes[i].Alias = conf.Processes[i].Process
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
|
|
|
}
|
|
|
|
|
2022-10-10 00:18:17 +04:00
|
|
|
func main() {
|
2024-07-07 17:23:29 +04:00
|
|
|
log.SetFlags(log.Llongfile)
|
2021-03-13 03:17:30 +04:00
|
|
|
flag.Parse()
|
|
|
|
|
2022-10-10 00:18:17 +04:00
|
|
|
if *showVersion {
|
2024-01-13 04:22:34 +04:00
|
|
|
fmt.Fprintln(os.Stderr, "httpprocprobed ver.", version, "\nCopyright (c) 2021-2024 Alexander \"Arav\" Andreev <me@arav.su>")
|
2024-07-07 17:23:29 +04:00
|
|
|
return
|
2021-03-13 03:17:30 +04:00
|
|
|
}
|
|
|
|
|
2022-10-10 00:18:17 +04:00
|
|
|
conf, err := LoadConfiguration(*configPath)
|
2021-03-13 03:17:30 +04:00
|
|
|
if err != nil {
|
2023-12-15 04:00:26 +04:00
|
|
|
log.Fatalf("Cannot load configuration file: %s\n", err)
|
2021-03-13 03:17:30 +04:00
|
|
|
}
|
|
|
|
|
2023-12-15 04:00:26 +04:00
|
|
|
router := http.NewServeMux()
|
2024-01-13 04:55:19 +04:00
|
|
|
router.HandleFunc("/processes", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
|
|
|
w.Header().Add("Allow", "GET")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
if err := json.NewEncoder(w).Encode(GetProcessesState(&conf.Processes)); err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
log.Printf("Failed to encode a process list: %s\n", err)
|
|
|
|
}
|
|
|
|
})
|
2021-11-03 23:52:57 +04:00
|
|
|
|
2023-12-15 04:00:26 +04:00
|
|
|
srv := &http.Server{
|
|
|
|
Addr: conf.ListenAddress,
|
|
|
|
Handler: router,
|
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
WriteTimeout: 5 * time.Second,
|
|
|
|
IdleTimeout: 10 * time.Second,
|
2021-11-03 23:52:57 +04:00
|
|
|
}
|
|
|
|
|
2023-12-15 04:00:26 +04:00
|
|
|
go func() {
|
|
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
|
log.Fatalf("ListenAndServe: %s\n", err)
|
2021-11-03 23:52:57 +04:00
|
|
|
}
|
2023-12-15 04:00:26 +04:00
|
|
|
}()
|
2024-01-13 04:41:46 +04:00
|
|
|
defer func() {
|
|
|
|
srv.SetKeepAlivesEnabled(false)
|
|
|
|
if err := srv.Shutdown(context.Background()); err != nil {
|
|
|
|
log.Fatalf("%s\n", err)
|
|
|
|
}
|
|
|
|
}()
|
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
|
|
|
|
2021-12-24 23:28:50 +04:00
|
|
|
for {
|
|
|
|
switch <-syssignal {
|
|
|
|
case os.Interrupt:
|
|
|
|
fallthrough
|
|
|
|
case syscall.SIGINT | syscall.SIGTERM:
|
2024-01-13 04:41:46 +04:00
|
|
|
return
|
2021-12-24 23:28:50 +04:00
|
|
|
case syscall.SIGHUP:
|
2022-10-10 00:18:17 +04:00
|
|
|
newconf, err := LoadConfiguration(*configPath)
|
2021-12-24 23:28:50 +04:00
|
|
|
if err != nil {
|
2022-01-02 05:13:20 +04:00
|
|
|
log.Fatalf("Failed to reload a list of processes from configuration: %s\n", err)
|
2021-12-24 23:28:50 +04:00
|
|
|
}
|
|
|
|
conf.Processes = newconf.Processes
|
|
|
|
}
|
2021-03-13 03:17:30 +04:00
|
|
|
}
|
|
|
|
}
|