Commit 4617de87 by 庄欣

s

parent 5de73c1f
一个进程监控小程序
\ No newline at end of file
{
"cmd": "ps -aux | grep php",
"php": "/usr/bin/php",
"watch": [
"task/listen",
"paycenter/queue",
"paycenter/web",
"queue/listen"
],
"path": "/home/code001/www/MepaiCoreBusiness/yii"
}
\ No newline at end of file
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os/exec"
"strings"
"time"
)
var configPath = flag.String("c", "", "Config file path");
type appConfig struct {
Cmd string `json:"cmd"`
Watch []string `json:"watch"`
Path string `json:"path"`
Php string `json:"php"`
}
func exec_shell(s string) (string, error) {
cmd := exec.Command("/bin/bash", "-c", s)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
return out.String(), err
}
func getConfig(configPath string) (appConfig, error) {
file, err := ioutil.ReadFile(configPath);
if err != nil {
panic(err)
}
var config appConfig;
err = json.Unmarshal(file, &config)
if err != nil {
panic(err)
}
return config, nil;
}
func main() {
defer func() {
if err := recover(); err != nil {
fmt.Println(err)
}
}()
flag.Parse();
config, err := getConfig(*configPath);
if err != nil {
fmt.Errorf(err.Error())
return;
}
out, _ := exec_shell(config.Cmd)
outs := strings.Split(out, "\n")
keys := make(map[string]bool);
for _, key := range config.Watch {
keys[key] = false;
}
for _,v := range outs {
for _, key := range config.Watch {
if strings.Index(v, key) >= 0 {
keys[key] = true;
}
}
}
var command *exec.Cmd
y,m,d := time.Now().Date()
f := fmt.Sprintf("%d-%d-%d", y,m,d);
for key,result := range keys {
if result == false {
command = exec.Command(config.Php, config.Path, key, "> /root/monitors/logs/monitor-"+f+".log")
command.Run()
}
}
}
\ No newline at end of file
module example.com/m/v2
go 1.15
require (
github.com/PuerkitoBio/goquery v1.6.1 // indirect
github.com/chromedp/chromedp v0.6.4
)
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"time"
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/network"
"github.com/chromedp/chromedp"
)
var flagPort = flag.Int("port", 8544, "port")
func main() {
flag.Parse()
// start cookie server
go cookieServer(fmt.Sprintf(":%d", *flagPort))
// create context
ctx, cancel := chromedp.NewContext(context.Background())
defer cancel()
// run task list
var res string
err := chromedp.Run(ctx, setcookies(
fmt.Sprintf("http://localhost:%d", *flagPort), &res,
"cookie1", "value1",
"cookie2", "value2",
))
if err != nil {
log.Fatal(err)
}
log.Printf("chrome received cookies: %s", res)
}
// cookieServer creates a simple HTTP server that logs any passed cookies.
func cookieServer(addr string) error {
mux := http.NewServeMux()
mux.HandleFunc("/", func(res http.ResponseWriter, req *http.Request) {
cookies := req.Cookies()
for i, cookie := range cookies {
log.Printf("from %s, server received cookie %d: %v", req.RemoteAddr, i, cookie)
}
buf, err := json.MarshalIndent(req.Cookies(), "", " ")
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(res, indexHTML, string(buf))
})
return http.ListenAndServe(addr, mux)
}
// setcookies returns a task to navigate to a host with the passed cookies set
// on the network request.
func setcookies(host string, res *string, cookies ...string) chromedp.Tasks {
if len(cookies)%2 != 0 {
panic("length of cookies must be divisible by 2")
}
return chromedp.Tasks{
chromedp.ActionFunc(func(ctx context.Context) error {
// create cookie expiration
expr := cdp.TimeSinceEpoch(time.Now().Add(180 * 24 * time.Hour))
// add cookies to chrome
for i := 0; i < len(cookies); i += 2 {
err := network.SetCookie(cookies[i], cookies[i+1]).
WithExpires(&expr).
WithDomain("localhost").
WithHTTPOnly(true).
Do(ctx)
if err != nil {
return err
}
}
return nil
}),
// navigate to site
chromedp.Navigate(host),
// read the returned values
chromedp.Text(`#result`, res, chromedp.ByID, chromedp.NodeVisible),
// read network values
chromedp.ActionFunc(func(ctx context.Context) error {
fmt.Println("111111111111111111111111111111111111111111111111111111111111111111111111")
cookies, err := network.GetAllCookies().Do(ctx)
if err != nil {
return err
}
for i, cookie := range cookies {
log.Printf("chrome cookie %d: %+v", i, cookie)
}
return nil
}),
}
}
const (
indexHTML = `<!doctype html>
<html>
<body>
<div id="result">%s</div>
</body>
</html>`
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment