2019-12-02 09:57:14 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-16 01:11:51 -05:00
|
|
|
"crypto/tls"
|
2021-01-15 11:45:49 -05:00
|
|
|
"fmt"
|
2019-12-02 09:57:14 -05:00
|
|
|
"log"
|
2021-01-15 11:45:49 -05:00
|
|
|
"net"
|
2021-01-16 01:11:51 -05:00
|
|
|
"net/http"
|
2021-01-24 08:17:37 -05:00
|
|
|
"os"
|
2019-12-02 09:57:14 -05:00
|
|
|
"os/exec"
|
2021-01-15 11:45:49 -05:00
|
|
|
"time"
|
2019-12-11 08:50:49 -05:00
|
|
|
|
2021-01-15 11:45:49 -05:00
|
|
|
"github.com/go-ping/ping"
|
2021-01-28 10:19:59 -05:00
|
|
|
"github.com/naiba/nezha/pkg/utils"
|
2021-02-27 06:24:19 -05:00
|
|
|
"github.com/shirou/gopsutil/v3/cpu"
|
2020-12-12 22:25:30 -05:00
|
|
|
"github.com/shirou/gopsutil/v3/disk"
|
2021-02-27 06:24:19 -05:00
|
|
|
"github.com/shirou/gopsutil/v3/host"
|
2019-12-02 09:57:14 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-01-20 20:37:29 -05:00
|
|
|
// icmp()
|
2021-01-16 05:04:47 -05:00
|
|
|
// tcpping()
|
2021-01-24 08:17:37 -05:00
|
|
|
// httpWithSSLInfo()
|
2021-02-27 06:24:19 -05:00
|
|
|
sysinfo()
|
|
|
|
// cmdExec()
|
2021-01-16 05:04:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func tcpping() {
|
|
|
|
start := time.Now()
|
|
|
|
conn, err := net.DialTimeout("tcp", "example.com:80", time.Second*10)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
conn.Write([]byte("ping\n"))
|
|
|
|
conn.Close()
|
|
|
|
fmt.Println(time.Now().Sub(start).Microseconds(), float32(time.Now().Sub(start).Microseconds())/1000.0)
|
|
|
|
}
|
|
|
|
|
2021-02-27 06:24:19 -05:00
|
|
|
func sysinfo() {
|
|
|
|
hi, _ := host.Info()
|
|
|
|
var cpuType string
|
|
|
|
if hi.VirtualizationSystem != "" {
|
|
|
|
cpuType = "Virtual"
|
|
|
|
} else {
|
|
|
|
cpuType = "Physical"
|
|
|
|
}
|
|
|
|
cpuModelCount := make(map[string]int)
|
|
|
|
ci, _ := cpu.Info()
|
|
|
|
for i := 0; i < len(ci); i++ {
|
|
|
|
cpuModelCount[ci[i].ModelName]++
|
|
|
|
}
|
|
|
|
var cpus []string
|
|
|
|
for model, count := range cpuModelCount {
|
|
|
|
cpus = append(cpus, fmt.Sprintf("%s %d %s Core", model, count, cpuType))
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
// 硬盘信息,不使用的原因是会重复统计 Linux、Mac
|
2021-01-16 05:04:47 -05:00
|
|
|
dparts, _ := disk.Partitions(false)
|
|
|
|
for _, part := range dparts {
|
|
|
|
u, _ := disk.Usage(part.Mountpoint)
|
|
|
|
if u != nil {
|
|
|
|
log.Printf("%s %d %d", part.Device, u.Total, u.Used)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func httpWithSSLInfo() {
|
2021-01-16 01:11:51 -05:00
|
|
|
// 跳过 SSL 检查
|
|
|
|
transCfg := &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
}
|
2021-01-20 20:37:29 -05:00
|
|
|
httpClient := &http.Client{Transport: transCfg, CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
}}
|
|
|
|
resp, err := httpClient.Get("http://mail.nai.ba")
|
|
|
|
fmt.Println(err, resp.StatusCode)
|
2021-01-16 01:11:51 -05:00
|
|
|
// SSL 证书信息获取
|
2021-01-20 20:37:29 -05:00
|
|
|
// c := cert.NewCert("expired-ecc-dv.ssl.com")
|
|
|
|
// fmt.Println(c.Error)
|
2021-01-16 05:04:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func icmp() {
|
|
|
|
pinger, err := ping.NewPinger("10.10.10.2")
|
2021-01-15 11:45:49 -05:00
|
|
|
if err != nil {
|
2021-01-16 05:04:47 -05:00
|
|
|
panic(err) // Blocks until finished.
|
2021-01-15 11:45:49 -05:00
|
|
|
}
|
2021-01-16 05:04:47 -05:00
|
|
|
pinger.Count = 3000
|
|
|
|
pinger.Timeout = 10 * time.Second
|
|
|
|
if err = pinger.Run(); err != nil {
|
2021-01-15 11:45:49 -05:00
|
|
|
panic(err)
|
|
|
|
}
|
2021-01-16 05:04:47 -05:00
|
|
|
fmt.Println(pinger.PacketsRecv, float32(pinger.Statistics().AvgRtt.Microseconds())/1000.0)
|
2019-12-02 09:57:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func cmdExec() {
|
2021-01-24 08:17:37 -05:00
|
|
|
execFrom, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-01-28 10:19:59 -05:00
|
|
|
var cmd *exec.Cmd
|
2021-01-28 22:59:35 -05:00
|
|
|
pg, err := utils.NewProcessExitGroup()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-01-28 10:19:59 -05:00
|
|
|
if utils.IsWindows() {
|
2021-01-29 01:29:31 -05:00
|
|
|
cmd = exec.Command("cmd", "/c", os.Args[1])
|
|
|
|
// cmd = exec.Command("cmd", "/c", execFrom+"/cmd/playground/example.sh hello asd")
|
2021-01-28 10:19:59 -05:00
|
|
|
} else {
|
|
|
|
cmd = exec.Command("sh", "-c", execFrom+`/cmd/playground/example.sh hello && \
|
2021-01-28 22:59:35 -05:00
|
|
|
echo world!`)
|
2021-01-28 10:19:59 -05:00
|
|
|
}
|
2021-01-28 22:59:35 -05:00
|
|
|
pg.AddProcess(cmd)
|
2021-01-28 21:40:57 -05:00
|
|
|
go func() {
|
2021-01-29 01:29:31 -05:00
|
|
|
time.Sleep(time.Second * 10)
|
2021-01-28 22:59:35 -05:00
|
|
|
if err = pg.Dispose(); err != nil {
|
|
|
|
panic(err)
|
2021-01-28 21:40:57 -05:00
|
|
|
}
|
2021-01-28 22:59:35 -05:00
|
|
|
fmt.Println("killed")
|
2021-01-28 21:40:57 -05:00
|
|
|
}()
|
2021-01-28 22:59:35 -05:00
|
|
|
output, err := cmd.Output()
|
|
|
|
log.Println("output:", string(output))
|
|
|
|
log.Println("err:", err)
|
2019-12-02 09:57:14 -05:00
|
|
|
}
|