From a1ef2d42d320f075c75e8cef52a879114441c103 Mon Sep 17 00:00:00 2001 From: sunty <1172534965@qq.com> Date: 星期三, 30 十月 2019 15:54:07 +0800 Subject: [PATCH] Merge remote-tracking branch 'origin/master' --- controllers/panTilt.go | 32 ++++ controllers/syssetcont.go | 46 +++++- config/test.yaml | 15 ++ scripts/netconfig | 122 +++++++++++++++++ extend/sys/system.go | 96 +++++++++++++ extend/util/util.go | 42 ------ extend/config/config.go | 56 ++++--- router/router.go | 6 8 files changed, 340 insertions(+), 75 deletions(-) diff --git a/config/test.yaml b/config/test.yaml index b75ad1b..4c84f61 100644 --- a/config/test.yaml +++ b/config/test.yaml @@ -19,6 +19,21 @@ channelCount: 0 #纭洏涓暟 diskCount: 2 + #Exec root command + sudoPassword: 123 + sysThresholds: + - + value: 60 + color: #13ce66 + - + value: 80 + color: #FF9C4A + - + value: 95 + color: #f53d3d + - + value: 100 + color: #5d0000 database: driver: sqlite name: sqlite3 diff --git a/controllers/panTilt.go b/controllers/panTilt.go new file mode 100644 index 0000000..5dd318c --- /dev/null +++ b/controllers/panTilt.go @@ -0,0 +1,32 @@ +package controllers + +import ( + "webserver/extend/code" + "webserver/extend/util" + + "github.com/gin-gonic/gin" +) + +type PanTiltController struct { +} + +type PTInstruct struct { + ChannelId string `json:"channelId"` //鏈嶅姟鍣╥d + PTZType string `json:"ptzType"` //杞鍛ㄦ湡 + PTZParam int32 `json:"ptzParam"` //寤舵椂鏃堕棿 +} + +func (controller PanTiltController) Controlling(c *gin.Context) { + var param PTInstruct + if err := c.BindJSON(¶m); err != nil { + util.ResponseFormat(c, code.RequestParamError, "鍙傛暟鏈夎") + return + } + + b := true + if b { + util.ResponseFormat(c, code.Success, "") + } else { + util.ResponseFormat(c, code.ComError, "淇濆瓨澶辫触") + } +} diff --git a/controllers/syssetcont.go b/controllers/syssetcont.go index afab0db..8034844 100644 --- a/controllers/syssetcont.go +++ b/controllers/syssetcont.go @@ -159,11 +159,13 @@ } else { resData["server_id"] = sysconf.GetServerId() resData["server_name"] = sysconf.GetServerName() - ipv4, mask, _ := util.GetLocalIP(config.Server.NetworkAdapter) - gateway, _ := util.GetDefaultRoute(config.Server.NetworkAdapter) + ipv4, mask, _ := sys.GetLocalIP(config.Server.NetworkAdapter) + gateway, _ := sys.GetDefaultRoute(config.Server.NetworkAdapter) + dns, _ := sys.GetDnsServer() resData["ip"] = ipv4 resData["subMask"] = mask resData["gateway"] = gateway + resData["dns"] = dns resData["deviceNum"] = config.Server.DeviceNum resData["deviceType"] = config.Server.DeviceType @@ -193,6 +195,14 @@ var api dbapi.SysSetApi paramBody := make(map[string]interface{}, 0) paramBody["serverName"] = dev_name + + // 璁剧疆ip + isOk, msg := sys.SetNetworkConfig(c.PostForm("ip"), c.PostForm("subMask"), c.PostForm("gateway"), c.PostForm("dns")) + if !isOk { + util.ResponseFormat(c, code.ComError, msg) + return + } + b, data := api.SaveServerInfo(paramBody) if b { // 鏌ヨ鏄惁瀛樺湪 util.ResponseFormat(c, code.Success, data) @@ -308,11 +318,6 @@ return } - if root := sys.CheckRootPermissions(); !root { - util.ResponseFormat(c, code.ServiceInsideError, "鏈嶅姟鍣ㄦ病鏈変慨鏀规椂闂寸殑鏉冮檺") - return - } - if args.TimeZone != "CST" && args.TimeZone != "UTC" { if r := sys.SetTimeZone(args.TimeZone); !r { util.ResponseFormat(c, code.RequestParamError, "鏃跺尯鍙傛暟閿欒") @@ -346,3 +351,30 @@ info := sys.GetSysInfo() util.ResponseFormat(c, code.UpdateSuccess, info) } + +// @Summary 鏌ヨ绯荤粺鐘舵�侀槇鍊艰缃� +// @Description 鑾峰彇褰撳墠绯荤粺鐨勮繍琛岀姸鎬侊紝CPU, GPU, 鍐呭瓨鐨勯槇鍊奸厤缃� +// @Produce json +// @Tags sysset +// @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}" +// @Failure 500 {string} json "{"code":500, msg:"杩斿洖閿欒淇℃伅", success:false}" +// @Router /data/api-v/sysset/sysThresholds [GET] +func (sset SysSetController) GetSysThresholds(c *gin.Context) { + util.ResponseFormat(c, code.UpdateSuccess, config.Server.SysThresholds) +} + +// @Summary 閲嶅惎绯荤粺 +// @Description 閲嶅惎鎿嶄綔绯荤粺 +// @Produce json +// @Tags sysset +// @Success 200 {string} json "{"code":200, msg:"鐩綍缁撴瀯鏁版嵁", success:true}" +// @Failure 500 {string} json "{"code":500, msg:"杩斿洖閿欒淇℃伅", success:false}" +// @Router /data/api-v/sysset/reboot [GET] +func (sset SysSetController) RebootOS(c *gin.Context) { + if isOk, msg := sys.Reboot(); !isOk { + util.ResponseFormat(c, code.UpdateFail, msg) + return + } + + util.ResponseFormat(c, code.Success, "姝e湪閲嶅惎") +} diff --git a/extend/config/config.go b/extend/config/config.go index 0a7b6b0..c775712 100644 --- a/extend/config/config.go +++ b/extend/config/config.go @@ -1,33 +1,41 @@ package config import ( - "github.com/spf13/viper" "log" + + "github.com/spf13/viper" ) +type threshold struct { + Value int `mapstructure: "value"` + Color string `mapstructure: "color"` +} type server struct { - Runmode string `mapstructure: "runmode"` - JwtSecret string `mapstructure: "jwtSecret"` - JwtExpire string `mapstructure: "jwtExpire"` - Url string `mapstructure: "url"` - AnalyServerId string `mapstructure: "analyServerId"` + Runmode string `mapstructure: "runmode"` + JwtSecret string `mapstructure: "jwtSecret"` + JwtExpire string `mapstructure: "jwtExpire"` + Url string `mapstructure: "url"` + AnalyServerId string `mapstructure: "analyServerId"` NetworkAdapter string `mapstructure: "networkAdapter"` - DeviceNum string `mapstructure: "deviceNum"` //璁惧缂栧彿 - DeviceType string `mapstructure: "deviceType"` //璁惧鍨嬪彿 - DeviceSerialNum string `mapstructure: "deviceSerialNum"` //璁惧搴忓垪鍙� - MasterVersion string `mapstructure: "masterVersion"` //涓绘帶鐗堟湰 - WebVersion string `mapstructure: "webVersion"` //web鐗堟湰 - ChannelCount string `mapstructure: "channelCount"` //閫氶亾涓暟 - DiskCount string `mapstructure: "diskCount"` //纭洏涓暟 + DeviceNum string `mapstructure: "deviceNum"` //璁惧缂栧彿 + DeviceType string `mapstructure: "deviceType"` //璁惧鍨嬪彿 + DeviceSerialNum string `mapstructure: "deviceSerialNum"` //璁惧搴忓垪鍙� + MasterVersion string `mapstructure: "masterVersion"` //涓绘帶鐗堟湰 + WebVersion string `mapstructure: "webVersion"` //web鐗堟湰 + ChannelCount string `mapstructure: "channelCount"` //閫氶亾涓暟 + DiskCount string `mapstructure: "diskCount"` //纭洏涓暟 + + SudoPassword string `mapstructure: "sudoPassword"` + SysThresholds []threshold `mapstructure: "sysThresholds"` } var Server = &server{} // wp add es 绱㈠紩 浠ュ強 IP port type esinfo struct { - Shards string `mapstructure:"shards"` - EsIndex esindexlist `mapstructure:"index"` + Shards string `mapstructure:"shards"` + EsIndex esindexlist `mapstructure:"index"` } type esindexlist struct { @@ -42,7 +50,7 @@ } type sopath struct { - Ip string `mapstructure:"ip"` + Ip string `mapstructure:"ip"` Port string `mapstructure:"port"` } @@ -51,18 +59,18 @@ var EsInfo = &esinfo{} type facedetect struct { - Ip string `mapstructure:"Ip"` - Port int `mapstructure:"port"` + Ip string `mapstructure:"Ip"` + Port int `mapstructure:"port"` } type dbpersoncompare struct { - Ip string `mapstructure:"ip"` - Port int `mapstructure:"port"` + Ip string `mapstructure:"ip"` + Port int `mapstructure:"port"` } type espersoncompare struct { - Port int `mapstructure:"port"` - Ips []string `mapstructure:"ips"` + Port int `mapstructure:"port"` + Ips []string `mapstructure:"ips"` } var DbPersonCompInfo = &dbpersoncompare{} @@ -84,8 +92,8 @@ } viper.UnmarshalKey("es", EsInfo) viper.UnmarshalKey("server", Server) - viper.UnmarshalKey("sopath",SoPath) + viper.UnmarshalKey("sopath", SoPath) viper.UnmarshalKey("facedetect", FaceDetectSet) - viper.UnmarshalKey("dbpersoncompare",DbPersonCompInfo) + viper.UnmarshalKey("dbpersoncompare", DbPersonCompInfo) viper.UnmarshalKey("espersoncompare", EsCompServerInfo) } diff --git a/extend/sys/system.go b/extend/sys/system.go index 563d85b..cccba7e 100644 --- a/extend/sys/system.go +++ b/extend/sys/system.go @@ -2,13 +2,24 @@ import ( "errors" + "os" "os/exec" + "strconv" + "webserver/extend/config" + "webserver/extend/util" "fmt" "net" "strings" "time" ) + +func execRootCommand(cmd string) ([]byte, error) { + pwd := config.Server.SudoPassword + cmdStr := fmt.Sprintf("echo %s | sudo -S %s", pwd, cmd) + + return exec.Command("/bin/sh", "-c", cmdStr).Output() +} // 妫�鏌� root鏉冮檺 func CheckRootPermissions() bool { @@ -18,6 +29,79 @@ } return true +} + +// 鑾峰彇鏈満缃戝崱IP +func GetLocalIP(networkName string) (ipv4 string, mask string, err error) { + interfaces, err := net.Interfaces() + if err != nil { + return "", "", err + } + + for _, i := range interfaces { + byName, err := net.InterfaceByName(i.Name) + if err != nil { + return "", "", err + } + addresses, err := byName.Addrs() + for _, v := range addresses { + if ipnet, ok := v.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if ipnet.IP.To4() != nil { + if byName.Name == networkName { + maskStr := ipnet.Mask.String() + mask64, _ := strconv.ParseUint(maskStr, 16, 32) + return ipnet.IP.String(), util.IpIntToString(int(mask64)), nil + } + } + } + } + } + return "", "", errors.New("ipv4 not found") +} + +// 鑾峰彇鏈満缃戝叧ip +func GetDefaultRoute(networkName string) (route string, err error) { + cmdStr := fmt.Sprintf("ip route show | grep -P \"^default.*%s\" | awk '{printf $3}'", networkName) + cmd := exec.Command("/bin/sh", "-c", cmdStr) + b, err := cmd.Output() + if err != nil { + return "", err + } + + return string(b), nil +} + +func GetDnsServer() (dns string, err error) { + cmd := exec.Command("/bin/sh", "-c", "cat /etc/resolv.conf | grep nameserver | head -1 |awk '{printf $2}'") + b, err := cmd.Output() + if err != nil { + return "", err + } + + return string(b), nil +} + +// 閰嶇疆鏈嶅姟鍣ㄧ綉缁� +func SetNetworkConfig(ipv4, netmask, gateway, dns string) (bool, string) { + networkConfigScript := "/opt/vasystem/bin/netconfig" + ifname := config.Server.NetworkAdapter + localIPv4, localNetMask, _ := GetLocalIP(ifname) + localGW, _ := GetDefaultRoute(ifname) + localDNS, _ := GetDnsServer() + + if localIPv4 == ipv4 && localNetMask == netmask && localGW == gateway && localDNS == dns { + return true, "" + } + + // 妫�鏌ユ枃浠舵槸鍚﹀瓨鍦� + if _, err := os.Stat(networkConfigScript); err != nil { + return false, "network Config Script not exist" + } + // # netconfig enp59s0f0 192.168.1.2 255.255.255.0 192.168.1.1 192.168.100.1 + cmdStr := fmt.Sprintf("%s %s %s %s %s %s", networkConfigScript, ifname, ipv4, netmask, gateway, dns) + stdout, err := execRootCommand(cmdStr) + + return err == nil, string(stdout) } // 閰嶇疆鏃跺尯 @@ -76,8 +160,10 @@ return false } - args := []string{"-s", newTime} - exec.Command("date", args...).Run() + // args := []string{"-s", newTime} + // exec.Command("date", args...).Run() + dateCMD := fmt.Sprintf("date -s \"%s\"", newTime) + execRootCommand(dateCMD) stopNTPCron() return true @@ -111,3 +197,9 @@ ntpdate := fmt.Sprintf("/usr/sbin/ntpdate %s", server) return true, exec.Command("/bin/sh", "-c", ntpdate).Run() } + +func Reboot() (bool, string) { + stdout, err := execRootCommand("reboot") + + return err == nil, string(stdout) +} diff --git a/extend/util/util.go b/extend/util/util.go index e680ee6..542dd2c 100644 --- a/extend/util/util.go +++ b/extend/util/util.go @@ -3,12 +3,9 @@ import ( "bytes" "encoding/json" - "errors" "image" "io/ioutil" - "net" "net/http" - "os/exec" "strconv" "webserver/extend/code" @@ -134,45 +131,6 @@ y1_new = int(i.Height) } return -} - -// 鑾峰彇鏈満缃戝崱IP -func GetLocalIP(networkName string) (ipv4 string, mask string, err error) { - interfaces, err := net.Interfaces() - if err != nil { - return "", "", err - } - - for _, i := range interfaces { - byName, err := net.InterfaceByName(i.Name) - if err != nil { - return "", "", err - } - addresses, err := byName.Addrs() - for _, v := range addresses { - if ipnet, ok := v.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { - if ipnet.IP.To4() != nil { - if byName.Name == networkName { - maskStr := ipnet.Mask.String() - mask64, _ := strconv.ParseUint(maskStr, 16, 32) - return ipnet.IP.String(), IpIntToString(int(mask64)), nil - } - } - } - } - } - return "", "", errors.New("ipv4 not found") -} - -func GetDefaultRoute(networkName string) (route string, err error) { - cmdStr := fmt.Sprintf("ip route show | grep -P \"^default.*%s\" | awk '{print $3}'", networkName) - cmd := exec.Command("/bin/sh", "-c", cmdStr) - b, err := cmd.Output() - if err != nil { - return "", err - } - - return string(b), nil } func StringIpToInt(ipstring string) int { diff --git a/router/router.go b/router/router.go index 440edef..abc6ffe 100644 --- a/router/router.go +++ b/router/router.go @@ -42,6 +42,7 @@ sysMenuController := new(controllers.SysMenuController) clusterController := new(controllers.ClusterController) sysRoleController := new(controllers.RoleController) + ptController := new(controllers.PanTiltController) sysApi := r.Group("/data/api-u/sys") { @@ -99,6 +100,8 @@ camera.POST("/updateRunEnable", cameraController.UpdateRunEnable) camera.POST("/changeRunType", cameraController.ChangeRunType) camera.GET("/getAllCamerasByServer", cameraController.GetAllCamerasByServer) + + camera.POST("/ptControl", ptController.Controlling) } cameraTaskArgsApi := r.Group(urlPrefix + "/cameraTaskArgs") @@ -203,6 +206,9 @@ vsset.POST("/updateClock", ssController.SetSysClock) vsset.GET("/ntpTest", ssController.TestNTPServer) vsset.GET("/sysinfo", ssController.GetSysInfo) + vsset.GET("/sysThresholds", ssController.GetSysThresholds) + + vsset.GET("/reboot", ssController.RebootOS) } //绠楁硶搴撴搷浣� diff --git a/scripts/netconfig b/scripts/netconfig new file mode 100644 index 0000000..6b9fae0 --- /dev/null +++ b/scripts/netconfig @@ -0,0 +1,122 @@ +#!/bin/bash + +# 绯荤粺缃戠粶閰嶇疆鏂囦欢 +NETWORK_CONFIG_FILE="/etc/network/interfaces" +LOG_FILE="/opt/vasystem/logs/netconfig.log" + +# basic绯荤粺鏈嶅姟鍏抽棴鑴氭湰 +SERVICE_STOP_CMD="" +# basic绯荤粺鏈嶅姟鍚姩鑴氭湰 +SERVICE_START_CMD="" + +INTERFACE="$1" +IPADDR="$2" +NETMASK="$3" +GATEWAY="$4" +DNSSERVER="$5" + +Usage(){ + cat <<EOF +$0 : missing operand +Usage: + $0 <ifname> <ipaddr> <netmask> <gateway> [dns-server] +EOF + exit 1 +} + +n_log() { + v_time=$(date "+%Y-%m-%d %H:%M:%S") + echo "$v_time $*" >>$LOG_FILE +} + +write_interfaces() +{ +echo "auto lo +iface lo inet loopback + +auto $INTERFACE +iface $INTERFACE inet static +address $IPADDR +netmask $NETMASK +gateway $GATEWAY +dns-nameserver $DNSSERVER +" > $NETWORK_CONFIG_FILE + +ret=$? +n_log "write_interfaces. ret: $ret" + +return $ret +} + +validate_ip() +{ + if [[ $1 =~ ^([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]]; then + return 0 + fi + + return 1 +} + +network_restart() +{ + # 閲嶅惎缃戠粶 + /etc/init.d/networking restart + ip addr flush dev $INTERFACE + ifdown $INTERFACE + ifup $INTERFACE + + n_log "network restart." + + sleep 3 +} + +service_restart() +{ + n_log "service restart." >>$LOG_FILE + + # 閲嶅惎basic鏈嶅姟 + su basic -c "sh /opt/startAllServices.sh &" +} + +# Start Execution + +n_log "Start NetConfig Execution, CMD: $*" +# 鏍¢獙鍙傛暟涓暟 +if [ $# -lt 4 ]; then + Usage +fi + +# 楠岃瘉鎺ュ彛鏄惁瀛樺湪 +ifconfig | grep -q $INTERFACE || exit 1 + +# 鏍¢獙IP鍚堟硶鎬� +i=0 +while [ $# -ne 0 ] +do + if [ $i -gt 0 -a $i -lt 4 ]; then + validate_ip $1 || exit 1 + fi + shift + let i+=1 +done + +if [ "$DNSSERVER" == "" ]; then + DNSSERVER=$GATEWAY +fi + +# 閰嶇疆鎺ュ彛 +write_interfaces || { + echo -n "write interfaces failure" + exit 1 +} + +# 閲嶅惎缃戠粶 +network_restart + +# 閲嶅惎鐩稿叧鏈嶅姟, 涓嶆洿鏀筰p鐨勬儏鍐典笅, 閲嶅惎缃戠粶鍗冲彲 +localip=$(/sbin/ifconfig $INTERFACE | grep inet | grep -v inet6 | awk '{printf $2}' | tr -d "addr:") +if [ "$localip" != "$IPADDR" ]; then + service_restart +fi + +exit 0 \ No newline at end of file -- Gitblit v1.8.0