del: 移除重复模块

This commit is contained in:
meilin.huang
2021-03-24 17:22:38 +08:00
parent 7ff730128d
commit a1141a405a
12 changed files with 1 additions and 1119 deletions

View File

@@ -1,127 +0,0 @@
package controllers
import (
"mayfly-go/base"
"mayfly-go/base/biz"
"mayfly-go/base/ctx"
"mayfly-go/base/rediscli"
"mayfly-go/base/utils"
"mayfly-go/mock-server/machine"
"mayfly-go/mock-server/models"
"net/http"
"github.com/gorilla/websocket"
)
type MachineController struct {
base.Controller
}
const machineKey = "ccbscf:machines"
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024 * 1024 * 10,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func (c *MachineController) Machines() {
c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} {
return rediscli.HGetAll(machineKey)
})
}
// 创建机器信息
func (c *MachineController) CreateMachine() {
c.Operation(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) {
machine := &models.Machine{}
c.UnmarshalBodyAndValid(machine)
machine.CreateMachine()
})
}
// @router /api/mock-datas/:method [delete]
func (c *MockController) DeleteMachine() {
c.Operation(ctx.NewReqCtx(false, "删除mock数据"), func(account *ctx.LoginAccount) {
models.DeleteMachine(c.Ctx.Input.Param(":ip"))
})
}
func (c *MachineController) Run() {
rc := ctx.NewReqCtx(true, "执行机器命令")
c.ReturnData(rc, func(account *ctx.LoginAccount) interface{} {
cmd := c.GetString("cmd")
biz.NotEmpty(cmd, "cmd不能为空")
rc.ReqParam = cmd
res, err := c.getCli().Run(cmd)
biz.BizErrIsNil(err, "执行命令失败")
return res
})
}
// 系统基本信息
func (c *MachineController) SysInfo() {
c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} {
res, err := c.getCli().GetSystemInfo()
biz.BizErrIsNil(err, "获取系统基本信息失败")
return res
})
}
// top命令信息
func (c *MachineController) Top() {
c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} {
return c.getCli().GetTop()
})
}
func (c *MachineController) GetProcessByName() {
c.ReturnData(ctx.NewNoLogReqCtx(true), func(account *ctx.LoginAccount) interface{} {
name := c.GetString("name")
biz.NotEmpty(name, "name不能为空")
res, err := c.getCli().GetProcessByName(name)
biz.BizErrIsNil(err, "获取失败")
return res
})
}
func (c *MachineController) WsSSH() {
wsConn, err := upgrader.Upgrade(c.Ctx.ResponseWriter, c.Ctx.Request, nil)
if err != nil {
panic(biz.NewBizErr("获取requst responsewirte错误"))
}
cols, _ := c.GetInt("cols", 80)
rows, _ := c.GetInt("rows", 40)
sws, err := machine.NewLogicSshWsSession(cols, rows, c.getCli(), wsConn)
if sws == nil {
panic(biz.NewBizErr("连接失败"))
}
//if wshandleError(wsConn, err) {
// return
//}
defer sws.Close()
quitChan := make(chan bool, 3)
sws.Start(quitChan)
go sws.Wait(quitChan)
<-quitChan
}
func (c *MachineController) GetMachineIp() string {
machineIp := c.Ctx.Input.Param(":ip")
biz.IsTrue(utils.StrLen(machineIp) > 0, "ip错误")
return machineIp
}
func (c *MachineController) getCli() *machine.Cli {
cli, err := machine.GetCli(c.GetMachineIp())
biz.BizErrIsNil(err, "获取客户端错误")
return cli
}

View File

@@ -10,7 +10,7 @@ import (
"mayfly-go/mock-server/controllers/form"
)
const key = "ccbscf:mock:data"
const key = "mock:data"
type MockController struct {
base.Controller

View File

@@ -1,192 +0,0 @@
package machine
import (
"errors"
"fmt"
"io"
"mayfly-go/base/biz"
"mayfly-go/base/utils"
"mayfly-go/mock-server/models"
"net"
"os"
"sync"
"time"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/terminal"
)
// 客户端信息
type Cli struct {
machine *models.Machine
// ssh客户端
client *ssh.Client
}
// 客户端缓存
var clientCache sync.Map
var mutex sync.Mutex
// 从缓存中获取客户端信息,不存在则查库,并新建
func GetCli(machineIp string) (*Cli, error) {
mutex.Lock()
defer mutex.Unlock()
load, ok := clientCache.Load(machineIp)
if ok {
return load.(*Cli), nil
}
cli, err := newClient(models.GetMachineByIp(machineIp))
if err != nil {
return nil, err
}
clientCache.LoadOrStore(machineIp, cli)
return cli, nil
}
//根据机器信息创建客户端对象
func newClient(machine *models.Machine) (*Cli, error) {
if machine == nil {
return nil, errors.New("机器不存在")
}
cli := new(Cli)
cli.machine = machine
err := cli.connect()
if err != nil {
return nil, err
}
return cli, nil
}
//连接
func (c *Cli) connect() error {
// 如果已经有client则直接返回
if c.client != nil {
return nil
}
m := c.machine
config := ssh.ClientConfig{
User: m.Username,
Auth: []ssh.AuthMethod{ssh.Password(m.Password)},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
Timeout: 5 * time.Second,
}
addr := fmt.Sprintf("%s:%d", m.Ip, m.Port)
sshClient, err := ssh.Dial("tcp", addr, &config)
if err != nil {
return err
}
c.client = sshClient
return nil
}
// 测试连接
func TestConn(m *models.Machine) (*ssh.Client, error) {
config := ssh.ClientConfig{
User: m.Username,
Auth: []ssh.AuthMethod{ssh.Password(m.Password)},
HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
return nil
},
Timeout: 5 * time.Second,
}
addr := fmt.Sprintf("%s:%d", m.Ip, m.Port)
sshClient, err := ssh.Dial("tcp", addr, &config)
if err != nil {
return nil, err
}
return sshClient, nil
}
// 关闭client和并从缓存中移除
func (c *Cli) Close() {
if c.client != nil {
c.client.Close()
}
if utils.StrLen(c.machine.Ip) > 0 {
clientCache.Delete(c.machine.Ip)
}
}
// 获取sftp client
func (c *Cli) GetSftpCli() *sftp.Client {
if c.client == nil {
if err := c.connect(); err != nil {
panic(biz.NewBizErr("连接ssh失败" + err.Error()))
}
}
client, serr := sftp.NewClient(c.client, sftp.MaxPacket(1<<15))
if serr != nil {
panic(biz.NewBizErr("获取sftp client失败" + serr.Error()))
}
return client
}
// 获取session
func (c *Cli) GetSession() (*ssh.Session, error) {
if c.client == nil {
if err := c.connect(); err != nil {
return nil, err
}
}
return c.client.NewSession()
}
//执行shell
//@param shell shell脚本命令
func (c *Cli) Run(shell string) (*string, error) {
session, err := c.GetSession()
if err != nil {
c.Close()
return nil, err
}
defer session.Close()
buf, rerr := session.CombinedOutput(shell)
if rerr != nil {
return nil, rerr
}
res := string(buf)
return &res, nil
}
//执行带交互的命令
func (c *Cli) RunTerminal(shell string, stdout, stderr io.Writer) error {
session, err := c.GetSession()
if err != nil {
return err
}
//defer session.Close()
fd := int(os.Stdin.Fd())
oldState, err := terminal.MakeRaw(fd)
if err != nil {
panic(err)
}
defer terminal.Restore(fd, oldState)
session.Stdout = stdout
session.Stderr = stderr
session.Stdin = os.Stdin
termWidth, termHeight, err := terminal.GetSize(fd)
if err != nil {
panic(err)
}
// Set up terminal modes
modes := ssh.TerminalModes{
ssh.ECHO: 1, // enable echoing
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
// Request pseudo terminal
if err := session.RequestPty("xterm-256color", termHeight, termWidth, modes); err != nil {
return err
}
return session.Run(shell)
}

View File

@@ -1,142 +0,0 @@
package machine
import (
"fmt"
"mayfly-go/base/utils"
"strings"
"testing"
)
func TestSSH(t *testing.T) {
//ssh.ListenAndServe("148.70.36.197")
//cli := New("148.70.36.197", "root", "gea&630_..91mn#", 22)
////output, err := cli.Run("free -h")
////fmt.Printf("%v\n%v", output, err)
//err := cli.RunTerminal("tail -f /usr/local/java/logs/eatlife-info.log", os.Stdout, os.Stdin)
//fmt.Println(err)
res := "top - 17:14:07 up 5 days, 6:30, 2 users, load average: 0.03, 0.04, 0.05\nTasks: 101 total, 1 running, 100 sleeping, 0 stopped, 0 zombie\n%Cpu(s): 6.2 us, 0.0 sy, 0.0 ni, 93.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st\nKiB Mem : 1882012 total, 73892 free, 770360 used, 1037760 buff/cache\nKiB Swap: 0 total, 0 free, 0 used. 933492 avail Mem"
split := strings.Split(res, "\n")
//var firstLine string
//for i := 0; i < len(split); i++ {
// if i == 0 {
// val := strings.Split(split[i], "top -")[1]
// vals := strings.Split(val, ",")
//
// }
//}
firstLine := strings.Split(strings.Split(split[0], "top -")[1], ",")
// 17:14:07 up 5 days
up := strings.Trim(strings.Split(firstLine[0], "up")[1], " ") + firstLine[1]
// 2 users
users := strings.Split(strings.Trim(firstLine[2], " "), " ")[0]
// load average: 0.03
oneMinLa := strings.Trim(strings.Split(strings.Trim(firstLine[3], " "), ":")[1], " ")
fiveMinLa := strings.Trim(firstLine[4], " ")
fietMinLa := strings.Trim(firstLine[5], " ")
fmt.Println(firstLine, up, users, oneMinLa, fiveMinLa, fietMinLa)
tasks := Parse(strings.Split(split[1], "Tasks:")[1])
cpu := Parse(strings.Split(split[2], "%Cpu(s):")[1])
mem := Parse(strings.Split(split[3], "KiB Mem :")[1])
fmt.Println(tasks, cpu, mem)
}
func Parse(val string) map[string]string {
res := make(map[string]string)
vals := strings.Split(val, ",")
for i := 0; i < len(vals); i++ {
trimData := strings.Trim(vals[i], " ")
keyValue := strings.Split(trimData, " ")
res[keyValue[1]] = keyValue[0]
}
return res
}
func TestTemplateRev(t *testing.T) {
temp := "hello my name is {name} hahahaha lihaiba {age} years old {public}"
str := "hello my name is hmlhmlhm 慌慌信息 hahahaha lihaiba 15 years old private protected"
//temp1 := " top - {up}, {users} users, load average: {loadavg}"
//str1 := " top - 17:14:07 up 5 days, 6:30, 2 users, load average: 0.03, 0.04, 0.05"
//taskTemp := "Tasks: {total} total, {running} running, {sleeping} sleeping, {stopped} stopped, {zombie} zombie"
//taskVal := "Tasks: 101 total, 1 running, 100 sleeping, 0 stopped, 0 zombie"
//nameRunne := []rune(str)
//index := strings.Index(temp, "{")
//ei := strings.Index(temp, "}") + 1
//next := temp[ei:]
//key := temp[index+1 : ei-1]
//value := SubString(str, index, UnicodeIndex(str, next))
res := make(map[string]interface{})
utils.ReverStrTemplate(temp, str, res)
fmt.Println(res)
}
//func ReverStrTemplate(temp, str string, res map[string]string) {
// index := UnicodeIndex(temp, "{")
// ei := UnicodeIndex(temp, "}") + 1
// next := temp[ei:]
// nextContain := UnicodeIndex(next, "{")
// nextIndexValue := next
// if nextContain != -1 {
// nextIndexValue = SubString(next, 0, nextContain)
// }
// key := temp[index+1 : ei-1]
// // 如果后面没有内容了,则取字符串的长度即可
// var valueLastIndex int
// if nextIndexValue == "" {
// valueLastIndex = StrLen(str)
// } else {
// valueLastIndex = UnicodeIndex(str, nextIndexValue)
// }
// value := SubString(str, index, valueLastIndex)
// res[key] = value
//
// if nextContain != -1 {
// ReverStrTemplate(next, SubString(str, UnicodeIndex(str, value)+StrLen(value), StrLen(str)), res)
// }
//}
//
//func StrLen(str string) int {
// return len([]rune(str))
//}
//
//func SubString(str string, begin, end int) (substr string) {
// // 将字符串的转换成[]rune
// rs := []rune(str)
// lth := len(rs)
//
// // 简单的越界判断
// if begin < 0 {
// begin = 0
// }
// if begin >= lth {
// begin = lth
// }
// if end > lth {
// end = lth
// }
//
// // 返回子串
// return string(rs[begin:end])
//}
//
//func UnicodeIndex(str, substr string) int {
// // 子串在字符串的字节位置
// result := strings.Index(str, substr)
// if result >= 0 {
// // 获得子串之前的字符串并转换成[]byte
// prefix := []byte(str)[0:result]
// // 将子串之前的字符串转换成[]rune
// rs := []rune(string(prefix))
// // 获得子串之前的字符串的长度,便是子串在字符串的字符位置
// result = len(rs)
// }
//
// return result
//}
func TestRunShellFile(t *testing.T) {
}

View File

@@ -1,34 +0,0 @@
package machine
import (
"io/ioutil"
"mayfly-go/base/biz"
)
const BasePath = "./machine/shell/"
const MonitorTemp = "cpuRate:{cpuRate}%,memRate:{memRate}%,sysLoad:{sysLoad}\n"
// shell文件内容缓存避免每次读取文件
var shellCache = make(map[string]string)
func (c *Cli) GetProcessByName(name string) (*string, error) {
return c.Run(getShellContent("sys_info"))
}
func (c *Cli) GetSystemInfo() (*string, error) {
return c.Run(getShellContent("system_info"))
}
// 获取shell内容
func getShellContent(name string) string {
cacheShell := shellCache[name]
if cacheShell != "" {
return cacheShell
}
bytes, err := ioutil.ReadFile(BasePath + name + ".sh")
biz.ErrIsNil(err, "获取shell文件失败")
shellStr := string(bytes)
shellCache[name] = shellStr
return shellStr
}

View File

@@ -1,23 +0,0 @@
#! /bin/bash
# Function: 根据输入的程序的名字过滤出所对应的PID并显示出详细信息如果有几个PID则全部显示
NAME=%s
N=`ps -aux | grep $NAME | grep -v grep | wc -l` ##统计进程总数
if [ $N -le 0 ];then
echo "该进程名没有运行!"
fi
i=1
while [ $N -gt 0 ]
do
echo "进程PID: `ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $2}'`"
echo "进程命令:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $11}'`"
echo "进程所属用户: `ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $1}'`"
echo "CPU占用率`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $3}'`%"
echo "内存占用率:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $4}'`%"
echo "进程开始运行的时刻:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $9}'`"
echo "进程运行的时间:` ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $11}'`"
echo "进程状态:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $8}'`"
echo "进程虚拟内存:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $5}'`"
echo "进程共享内存:`ps -aux | grep $NAME | grep -v grep | awk 'NR=='$i'{print $0}'| awk '{print $6}'`"
echo "***************************************************************"
let N-- i++
done

View File

@@ -1,13 +0,0 @@
# 获取监控信息
function get_monitor_info() {
cpu_rate=$(cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1)
mem_rate=$(free -m | sed -n '2p' | awk '{print""($3/$2)*100"%"}')
sys_load=$(uptime | cut -d: -f5)
cat <<EOF | column -t
cpuRate:${cpu_rate},memRate:${mem_rate},sysLoad:${sys_load}
EOF
}
get_monitor_info

View File

@@ -1,192 +0,0 @@
#!/bin/bash
# func:sys info check
[ $(id -u) -ne 0 ] && echo "请用root用户执行此脚本" && exit 1
sysversion=$(rpm -q centos-release | cut -d- -f3)
line="-------------------------------------------------"
# 获取系统cpu信息
function get_cpu_info() {
Physical_CPUs=$(grep "physical id" /proc/cpuinfo | sort | uniq | wc -l)
Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l)
CPU_Kernels=$(grep "cores" /proc/cpuinfo | uniq | awk -F ': ' '{print $2}')
CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq)
CPU_Arch=$(uname -m)
cpu_usage=$(cat /proc/stat | awk '/cpu/{printf("%.2f%\n"), ($2+$4)*100/($2+$4+$5)}' | awk '{print $0}' | head -1)
#echo -e '\033[32m CPU信息\033[0m'
echo -e ' CPU信息'
cat <<EOF | column -t
物理CPU个数: $Physical_CPUs
逻辑CPU个数: $Virt_CPUs
每CPU核心数: $CPU_Kernels
CPU型号: $CPU_Type
CPU架构: $CPU_Arch
CPU使用率: $cpu_usage
EOF
}
# 获取系统内存信息
function get_mem_info() {
Total=$(free -m | sed -n '2p' | awk '{print $2"M"}')
Used=$(free -m | sed -n '2p' | awk '{print $3"M"}')
Rate=$(free -m | sed -n '2p' | awk '{print""($3/$2)*100"%"}')
echo -e ' 内存信息:'
cat <<EOF | column -t
内存总容量:$Total
内存已使用:$Used
内存使用率:$Rate
EOF
}
# 获取系统网络信息
function get_net_info() {
pri_ipadd=$(ifconfig | awk 'NR==2{print $2}')
#pub_ipadd=$(curl ip.sb 2>&1)
pub_ipadd=$(curl -s http://ddns.oray.com/checkip | awk -F ":" '{print $2}' | awk -F "<" '{print $1}' | awk '{print $1}')
gateway=$(ip route | grep default | awk '{print $3}')
mac_info=$(ip link | egrep -v "lo" | grep link | awk '{print $2}')
dns_config=$(egrep 'nameserver' /etc/resolv.conf)
route_info=$(route -n)
echo -e ' IP信息'
cat <<EOF | column -t
系统公网地址: ${pub_ipadd}
系统私网地址: ${pri_ipadd}
网关地址: ${gateway}
MAC地址: ${mac_info}
路由信息:
${route_info}
DNS 信息:
${dns_config}
EOF
}
# 获取系统磁盘信息
function get_disk_info() {
disk_info=$(fdisk -l | grep "Disk /dev" | cut -d, -f1)
disk_use=$(df -hTP | awk '$2!="tmpfs"{print}')
disk_inode=$(df -hiP | awk '$1!="tmpfs"{print}')
echo -e ' 磁盘信息:'
cat <<EOF
${disk_info}
磁盘使用:
${disk_use}
inode信息:
${disk_inode}
EOF
}
# 获取系统信息
function get_systatus_info() {
sys_os=$(uname -o)
sys_release=$(cat /etc/redhat-release)
sys_kernel=$(uname -r)
sys_hostname=$(hostname)
sys_selinux=$(getenforce)
sys_lang=$(echo $LANG)
sys_lastreboot=$(who -b | awk '{print $3,$4}')
sys_runtime=$(uptime | awk '{print $3,$4}' | cut -d, -f1)
sys_time=$(date)
sys_load=$(uptime | cut -d: -f5)
echo -e ' 系统信息:'
cat <<EOF | column -t
系统: ${sys_os}
发行版本: ${sys_release}
系统内核: ${sys_kernel}
主机名: ${sys_hostname}
selinux状态: ${sys_selinux}
系统语言: ${sys_lang}
系统当前时间: ${sys_time}
系统最后重启时间: ${sys_lastreboot}
系统运行时间: ${sys_runtime}
系统负载: ${sys_load}
---------------------------------------
EOF
}
# 获取服务信息
function get_service_info() {
port_listen=$(netstat -lntup | grep -v "Active Internet")
kernel_config=$(sysctl -p 2>/dev/null)
if [ ${sysversion} -gt 6 ]; then
service_config=$(systemctl list-unit-files --type=service --state=enabled | grep "enabled")
run_service=$(systemctl list-units --type=service --state=running | grep ".service")
else
service_config=$(/sbin/chkconfig | grep -E ":on|:启用" | column -t)
run_service=$(/sbin/service --status-all | grep -E "running")
fi
echo -e ' 服务启动配置:'
cat <<EOF
${service_config}
${line}
运行的服务:
${run_service}
${line}
监听端口:
${port_listen}
${line}
内核参考配置:
${kernel_config}
EOF
}
function get_sys_user() {
login_user=$(awk -F: '{if ($NF=="/bin/bash") print $0}' /etc/passwd)
ssh_config=$(egrep -v "^#|^$" /etc/ssh/sshd_config)
sudo_config=$(egrep -v "^#|^$" /etc/sudoers | grep -v "^Defaults")
host_config=$(egrep -v "^#|^$" /etc/hosts)
crond_config=$(for cronuser in /var/spool/cron/*; do
ls ${cronuser} 2>/dev/null | cut -d/ -f5
egrep -v "^$|^#" ${cronuser} 2>/dev/null
echo ""
done)
echo -e ' 系统登录用户:'
cat <<EOF
${login_user}
${line}
ssh 配置信息:
${ssh_config}
${line}
sudo 配置用户:
${sudo_config}
${line}
定时任务配置:
${crond_config}
${line}
hosts 信息:
${host_config}
EOF
}
function process_top_info() {
top_title=$(top -b n1 | head -7 | tail -1)
cpu_top10=$(top b -n1 | head -17 | tail -10)
mem_top10=$(top -b n1 | head -17 | tail -10 | sort -k10 -r)
echo -e ' CPU占用top10'
cat <<EOF
${top_title}
${cpu_top10}
EOF
echo -e ' 内存占用top10'
cat <<EOF
${top_title}
${mem_top10}
EOF
}
function sys_check() {
get_systatus_info
echo ${line}
get_cpu_info
echo ${line}
get_mem_info
echo ${line}
# get_net_info
# echo ${line}
get_disk_info
echo ${line}
get_service_info
echo ${line}
# get_sys_user
# echo ${line}
process_top_info
}
sys_check

View File

@@ -1,41 +0,0 @@
# 获取系统cpu信息
function get_cpu_info() {
Physical_CPUs=$(grep "physical id" /proc/cpuinfo | sort | uniq | wc -l)
Virt_CPUs=$(grep "processor" /proc/cpuinfo | wc -l)
CPU_Kernels=$(grep "cores" /proc/cpuinfo | uniq | awk -F ': ' '{print $2}')
CPU_Type=$(grep "model name" /proc/cpuinfo | awk -F ': ' '{print $2}' | sort | uniq)
CPU_Arch=$(uname -m)
echo -e '\n-------------------------- CPU信息 --------------------------'
cat <<EOF | column -t
物理CPU个数: $Physical_CPUs
逻辑CPU个数: $Virt_CPUs
每CPU核心数: $CPU_Kernels
CPU型号: $CPU_Type
CPU架构: $CPU_Arch
EOF
}
# 获取系统信息
function get_systatus_info() {
sys_os=$(uname -o)
sys_release=$(cat /etc/redhat-release)
sys_kernel=$(uname -r)
sys_hostname=$(hostname)
sys_selinux=$(getenforce)
sys_lang=$(echo $LANG)
sys_lastreboot=$(who -b | awk '{print $3,$4}')
echo -e '-------------------------- 系统信息 --------------------------'
cat <<EOF | column -t
系统: ${sys_os}
发行版本: ${sys_release}
系统内核: ${sys_kernel}
主机名: ${sys_hostname}
selinux状态: ${sys_selinux}
系统语言: ${sys_lang}
系统最后重启时间: ${sys_lastreboot}
EOF
}
get_systatus_info
#echo -e "\n"
get_cpu_info

View File

@@ -1,105 +0,0 @@
package machine
import (
"mayfly-go/base/biz"
"mayfly-go/base/utils"
"strconv"
"strings"
)
type SystemVersion struct {
Version string
}
func (c *Cli) GetSystemVersion() *SystemVersion {
res, _ := c.Run("cat /etc/redhat-release")
return &SystemVersion{
Version: *res,
}
}
//top - 17:14:07 up 5 days, 6:30, 2 users, load average: 0.03, 0.04, 0.05
//Tasks: 101 total, 1 running, 100 sleeping, 0 stopped, 0 zombie
//%Cpu(s): 6.2 us, 0.0 sy, 0.0 ni, 93.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
//KiB Mem : 1882012 total, 73892 free, 770360 used, 1037760 buff/cache
//KiB Swap: 0 total, 0 free, 0 used. 933492 avail Mem
type Top struct {
Time string `json:"time"`
// 从本次开机到现在经过的时间
Up string `json:"up"`
// 当前有几个用户登录到该机器
NowUsers int `json:"nowUsers"`
// load average: 0.03, 0.04, 0.05 (系统1分钟、5分钟、15分钟内的平均负载值)
OneMinLoadavg float32 `json:"oneMinLoadavg"`
FiveMinLoadavg float32 `json:"fiveMinLoadavg"`
FifteenMinLoadavg float32 `json:"fifteenMinLoadavg"`
// 进程总数
TotalTask int `json:"totalTask"`
// 正在运行的进程数对应状态TASK_RUNNING
RunningTask int `json:"runningTask"`
SleepingTask int `json:"sleepingTask"`
StoppedTask int `json:"stoppedTask"`
ZombieTask int `json:"zombieTask"`
// 进程在用户空间user消耗的CPU时间占比不包含调整过优先级的进程
CpuUs float32 `json:"cpuUs"`
// 进程在内核空间system消耗的CPU时间占比
CpuSy float32 `json:"cpuSy"`
// 调整过用户态优先级的niced进程的CPU时间占比
CpuNi float32 `json:"cpuNi"`
// 空闲的idleCPU时间占比
CpuId float32 `json:"cpuId"`
// 等待waitI/O完成的CPU时间占比
CpuWa float32 `json:"cpuWa"`
// 处理硬中断hardware interrupt的CPU时间占比
CpuHi float32 `json:"cpuHi"`
// 处理硬中断hardware interrupt的CPU时间占比
CpuSi float32 `json:"cpuSi"`
// 当Linux系统是在虚拟机中运行时等待CPU资源的时间steal time占比
CpuSt float32 `json:"cpuSt"`
TotalMem int `json:"totalMem"`
FreeMem int `json:"freeMem"`
UsedMem int `json:"usedMem"`
CacheMem int `json:"cacheMem"`
TotalSwap int `json:"totalSwap"`
FreeSwap int `json:"freeSwap"`
UsedSwap int `json:"usedSwap"`
AvailMem int `json:"availMem"`
}
func (c *Cli) GetTop() *Top {
res, _ := c.Run("top -b -n 1 | head -5")
topTemp := "top - {upAndUsers}, load average: {loadavg}\n" +
"Tasks:{totalTask} total,{runningTask} running,{sleepingTask} sleeping,{stoppedTask} stopped,{zombieTask} zombie\n" +
"%Cpu(s):{cpuUs} us,{cpuSy} sy,{cpuNi} ni,{cpuId} id,{cpuWa} wa,{cpuHi} hi,{cpuSi} si,{cpuSt} st\n" +
"KiB Mem :{totalMem} total,{freeMem} free,{usedMem} used,{cacheMem} buff/cache\n" +
"KiB Swap:{totalSwap} total,{freeSwap} free,{usedSwap} used. {availMem} avail Mem \n"
resMap := make(map[string]interface{})
utils.ReverStrTemplate(topTemp, *res, resMap)
//17:14:07 up 5 days, 6:30, 2
timeUpAndUserStr := resMap["upAndUsers"].(string)
timeUpAndUser := strings.Split(timeUpAndUserStr, "up")
time := utils.StrTrim(timeUpAndUser[0])
upAndUsers := strings.Split(timeUpAndUser[1], ",")
up := utils.StrTrim(upAndUsers[0]) + upAndUsers[1]
users, _ := strconv.Atoi(utils.StrTrim(strings.Split(utils.StrTrim(upAndUsers[2]), " ")[0]))
// 0.03, 0.04, 0.05
loadavgs := strings.Split(resMap["loadavg"].(string), ",")
oneMinLa, _ := strconv.ParseFloat(loadavgs[0], 32)
fiveMinLa, _ := strconv.ParseFloat(utils.StrTrim(loadavgs[1]), 32)
fifMinLa, _ := strconv.ParseFloat(utils.StrTrim(loadavgs[2]), 32)
top := &Top{Time: time, Up: up, NowUsers: users, OneMinLoadavg: float32(oneMinLa), FiveMinLoadavg: float32(fiveMinLa), FifteenMinLoadavg: float32(fifMinLa)}
err := utils.Map2Struct(resMap, top)
biz.BizErrIsNil(err, "解析top出错")
return top
}
type Status struct {
// 系统版本
SysVersion SystemVersion
// top信息
Top Top
}

View File

@@ -1,212 +0,0 @@
package machine
import (
"bytes"
"encoding/json"
"io"
"sync"
"time"
"github.com/beego/beego/v2/core/logs"
"github.com/gorilla/websocket"
"golang.org/x/crypto/ssh"
)
type safeBuffer struct {
buffer bytes.Buffer
mu sync.Mutex
}
func (w *safeBuffer) Write(p []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.buffer.Write(p)
}
func (w *safeBuffer) Bytes() []byte {
w.mu.Lock()
defer w.mu.Unlock()
return w.buffer.Bytes()
}
func (w *safeBuffer) Reset() {
w.mu.Lock()
defer w.mu.Unlock()
w.buffer.Reset()
}
const (
wsMsgCmd = "cmd"
wsMsgResize = "resize"
)
type WsMsg struct {
Type string `json:"type"`
Msg string `json:"msg"`
Cols int `json:"cols"`
Rows int `json:"rows"`
}
type LogicSshWsSession struct {
stdinPipe io.WriteCloser
comboOutput *safeBuffer //ssh 终端混合输出
logBuff *safeBuffer //保存session的日志
inputFilterBuff *safeBuffer //用来过滤输入的命令和ssh_filter配置对比的
session *ssh.Session
wsConn *websocket.Conn
}
func NewLogicSshWsSession(cols, rows int, cli *Cli, wsConn *websocket.Conn) (*LogicSshWsSession, error) {
sshSession, err := cli.GetSession()
if err != nil {
return nil, err
}
stdinP, err := sshSession.StdinPipe()
if err != nil {
return nil, err
}
comboWriter := new(safeBuffer)
logBuf := new(safeBuffer)
inputBuf := new(safeBuffer)
//ssh.stdout and stderr will write output into comboWriter
sshSession.Stdout = comboWriter
sshSession.Stderr = comboWriter
modes := ssh.TerminalModes{
ssh.ECHO: 1, // disable echo
ssh.TTY_OP_ISPEED: 14400, // input speed = 14.4kbaud
ssh.TTY_OP_OSPEED: 14400, // output speed = 14.4kbaud
}
// Request pseudo terminal
if err := sshSession.RequestPty("xterm", rows, cols, modes); err != nil {
return nil, err
}
// Start remote shell
if err := sshSession.Shell(); err != nil {
return nil, err
}
//sshSession.Run("top")
return &LogicSshWsSession{
stdinPipe: stdinP,
comboOutput: comboWriter,
logBuff: logBuf,
inputFilterBuff: inputBuf,
session: sshSession,
wsConn: wsConn,
}, nil
}
//Close 关闭
func (sws *LogicSshWsSession) Close() {
if sws.session != nil {
sws.session.Close()
}
if sws.logBuff != nil {
sws.logBuff = nil
}
if sws.comboOutput != nil {
sws.comboOutput = nil
}
}
func (sws *LogicSshWsSession) Start(quitChan chan bool) {
go sws.receiveWsMsg(quitChan)
go sws.sendComboOutput(quitChan)
}
//receiveWsMsg receive websocket msg do some handling then write into ssh.session.stdin
func (sws *LogicSshWsSession) receiveWsMsg(exitCh chan bool) {
wsConn := sws.wsConn
//tells other go routine quit
defer setQuit(exitCh)
for {
select {
case <-exitCh:
return
default:
//read websocket msg
_, wsData, err := wsConn.ReadMessage()
if err != nil {
logs.Error("reading webSocket message failed: ", err)
return
}
//unmashal bytes into struct
msgObj := WsMsg{}
if err := json.Unmarshal(wsData, &msgObj); err != nil {
logs.Error("unmarshal websocket message failed", err)
}
switch msgObj.Type {
case wsMsgResize:
//handle xterm.js size change
if msgObj.Cols > 0 && msgObj.Rows > 0 {
if err := sws.session.WindowChange(msgObj.Rows, msgObj.Cols); err != nil {
logs.Error("ssh pty change windows size failed")
}
}
case wsMsgCmd:
//handle xterm.js stdin
//decodeBytes, err := base64.StdEncoding.DecodeString(msgObj.Cmd)
//if err != nil {
// logs.Error("websock cmd string base64 decoding failed")
// //panic(base.NewBizErr("websock cmd string base64 decoding failed"))
//}
sws.sendWebsocketInputCommandToSshSessionStdinPipe([]byte(msgObj.Msg))
}
}
}
}
//sendWebsocketInputCommandToSshSessionStdinPipe
func (sws *LogicSshWsSession) sendWebsocketInputCommandToSshSessionStdinPipe(cmdBytes []byte) {
if _, err := sws.stdinPipe.Write(cmdBytes); err != nil {
logs.Error("ws cmd bytes write to ssh.stdin pipe failed")
}
}
func (sws *LogicSshWsSession) sendComboOutput(exitCh chan bool) {
wsConn := sws.wsConn
//todo 优化成一个方法
//tells other go routine quit
defer setQuit(exitCh)
//every 120ms write combine output bytes into websocket response
tick := time.NewTicker(time.Millisecond * time.Duration(60))
//for range time.Tick(120 * time.Millisecond){}
defer tick.Stop()
for {
select {
case <-tick.C:
if sws.comboOutput == nil {
return
}
bs := sws.comboOutput.Bytes()
if len(bs) > 0 {
err := wsConn.WriteMessage(websocket.TextMessage, bs)
if err != nil {
logs.Error("ssh sending combo output to webSocket failed")
}
_, err = sws.logBuff.Write(bs)
if err != nil {
logs.Error("combo output to log buffer failed")
}
sws.comboOutput.buffer.Reset()
}
case <-exitCh:
return
}
}
}
func (sws *LogicSshWsSession) Wait(quitChan chan bool) {
if err := sws.session.Wait(); err != nil {
setQuit(quitChan)
}
}
func (sws *LogicSshWsSession) LogString() string {
return sws.logBuff.buffer.String()
}
func setQuit(ch chan bool) {
ch <- true
}

View File

@@ -1,37 +0,0 @@
package models
import (
"encoding/json"
"mayfly-go/base/biz"
"mayfly-go/base/rediscli"
)
const machineKey = "ccbscf:machines"
type Machine struct {
Name string `json:"name"`
Ip string `json:"ip"` // IP地址
Username string `json:"username"` // 用户名
Password string `json:"-"`
Port int `json:"port"` // 端口号
}
func (c *Machine) CreateMachine() {
biz.IsTrue(!rediscli.HExist(machineKey, c.Ip), "该机器已存在")
val, _ := json.Marshal(c)
rediscli.HSet(machineKey, c.Ip, val)
}
func DeleteMachine(ip string) {
rediscli.HDel(machineKey, ip)
}
func GetMachineByIp(ip string) *Machine {
machine := &Machine{}
json.Unmarshal([]byte(rediscli.HGet(machineKey, ip)), machine)
return machine
}
func GetMachineList() map[string]string {
return rediscli.HGetAll(machineKey)
}