Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tools
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
庄欣
tools
Commits
4617de87
Commit
4617de87
authored
Jan 25, 2021
by
庄欣
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
s
parent
5de73c1f
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
214 additions
and
0 deletions
+214
-0
monitor/README.MD
+2
-0
monitor/app.json
+12
-0
monitor/main.go
+81
-0
t2/go.mod
+8
-0
t2/main.go
+111
-0
No files found.
monitor/README.MD
0 → 100644
View file @
4617de87
一个进程监控小程序
\ No newline at end of file
monitor/app.json
0 → 100644
View file @
4617de87
{
"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
monitor/main.go
0 → 100644
View file @
4617de87
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
t2/go.mod
0 → 100644
View file @
4617de87
module example.com/m/v2
go 1.15
require (
github.com/PuerkitoBio/goquery v1.6.1 // indirect
github.com/chromedp/chromedp v0.6.4
)
t2/main.go
0 → 100644
View file @
4617de87
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>`
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment