吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 15182|回复: 109
收起左侧

[Windows] 自用电脑状态监控小程序

  [复制链接]
vsyour 发表于 2022-8-26 14:12
本帖最后由 vsyour 于 2022-9-6 15:52 编辑

自用电脑状态监控小程序
功能:
监控当前电脑使用的 内存CPU硬盘 使用率信息, 超过设定阈值通过钉钉发送通知信息

主机状态监控程序网络版(2022年09月06日发布)
https://www.52pojie.cn/thread-1684879-1-1.html

运行截图:
运行截图.jpg


下载: https://wwn.lanzoub.com/iGDWh0aazoaf 密码:52pj
下载:https://vsyour.lanzoub.com/b02jjx4mf 密码:8igx
链接: https://pan.baidu.com/s/1M4-BtyYrYhXor6vPdpmWBQ 提取码: 52pj


源代码:
[Golang] 纯文本查看 复制代码
package main

import (
        "fmt"
        "os"
        "time"

        "github.com/shirou/gopsutil/v3/cpu"
        "github.com/shirou/gopsutil/v3/disk"
        "github.com/shirou/gopsutil/v3/mem"
        "github.com/spf13/viper"
        "github.com/wanghuiyt/ding"
)

// [url=https://github.com/wanghuiyt/ding]https://github.com/wanghuiyt/ding[/url]
// 全局配置变量
var config *viper.Viper

func GetCpuPercent() float64 {
        percent, _ := cpu.Percent(time.Second, false)
        return percent[0]
}

func GetMemPercent() float64 {
        memInfo, _ := mem.VirtualMemory()
        return memInfo.UsedPercent
}

func GetDiskPercent() float64 {
        parts, _ := disk.Partitions(true)
        diskInfo, _ := disk.Usage(parts[1].Mountpoint)
        return diskInfo.UsedPercent
}

func sendToDingDing(sendContent string) {
        // 接收string类型并发送内容

        d := ding.Webhook{
                AccessToken: config.GetString("dingding.AccessToken"),
                Secret:      config.GetString("dingding.Secret"),
                EnableAt:    true, // 开启艾特
                AtAll:       true, // 艾特所有人
        }
        err := d.SendMessage(sendContent)
        if err != nil {
                fmt.Println(err)
        }

}

func checkItem() {
        checkItems := config.GetStringMapString("checkItems")
        // fmt.Println(checkItems)
        for item, _ := range checkItems {
                cpuPercent := int32(GetCpuPercent())
                memPercent := int32(GetMemPercent())
                diskPercent := int32(GetDiskPercent())

                if item == "cpu" && cpuPercent > config.GetInt32(fmt.Sprintf("checkItems.%s", item)) {
                        timeStr := time.Now().Format("2006-01-02 15:04:05")
                        sendMessageContent := fmt.Sprintf("[%v] %v 使用率超过设定的[%v%%],当前使用率[%v%%]\n", timeStr, item, config.GetString(fmt.Sprintf("checkItems.%s", item)), cpuPercent)
                        sendToDingDing(sendMessageContent)

                        fmt.Printf("[%v] %v 使用率超过设定的[%v%%] ,当前使用率[%v%%]\n", timeStr, item, config.GetString(fmt.Sprintf("checkItems.%s", item)), cpuPercent)
                }

                if item == "mem" && memPercent > config.GetInt32(fmt.Sprintf("checkItems.%s", item)) {
                        timeStr := time.Now().Format("2006-01-02 15:04:05")
                        sendMessageContent := fmt.Sprintf("[%v] %v 使用率超过设定的[%v%%],当前使用率[%v%%] \n", timeStr, item, config.GetString(fmt.Sprintf("checkItems.%s", item)), memPercent)
                        sendToDingDing(sendMessageContent)

                        fmt.Printf("[%v] %v 使用率超过设定的[%v%%],当前使用率[%v%%] \n", timeStr, item, config.GetString(fmt.Sprintf("checkItems.%s", item)), memPercent)
                }

                if item == "disk" && diskPercent > config.GetInt32(fmt.Sprintf("checkItems.%s", item)) {
                        timeStr := time.Now().Format("2006-01-02 15:04:05")
                        sendMessageContent := fmt.Sprintf("[%v] %v 使用率超过设定的[%v%%],当前使用率[%v%%] \n", timeStr, item, config.GetString(fmt.Sprintf("checkItems.%s", item)), diskPercent)
                        sendToDingDing(sendMessageContent)

                        fmt.Printf("[%v] %v 使用率超过设定的[%v%%],当前使用率[%v%%] \n", timeStr, item, config.GetString(fmt.Sprintf("checkItems.%s", item)), diskPercent)
                }
        }
}

func main() {
        //获取项目的执行路径
        path, err := os.Getwd()
        if err != nil {
                panic(err)
        }
        config = viper.New()
        config.AddConfigPath(path)     //设置读取的文件路径
        config.SetConfigName("config") //设置读取的文件名
        config.SetConfigType("yaml")   //设置文件的类型
        //尝试进行配置读取
        if err := config.ReadInConfig(); err != nil {
                panic(err)
        }
        sleepTime := config.GetInt32("checkTime")
        fmt.Printf("循环间隔:%v秒\n", sleepTime)

        for {
                checkItem()
                timeStr := time.Now().Format("2006-01-02 15:04:05")
                fmt.Printf("[%v] 52pojie_vsyour提醒: 本次检测已完成, [%v秒] 后再次检测!\n", timeStr, sleepTime)
                time.Sleep(time.Duration(sleepTime) * time.Second)
        }

}

免费评分

参与人数 20吾爱币 +20 热心值 +16 收起 理由
jdxjwz + 1 + 1 热心回复!
wuhuaipojie + 1 谢谢@Thanks!
Tnl2009001 + 1 + 1 热心回复!
Jakenb + 1 + 1 谢谢@Thanks!
夜游星河 + 1 + 1 我很赞同!
7086pp + 1 + 1 我很赞同!
silent12 + 1 谢谢@Thanks!
ab135792 + 1 + 1 谢谢@Thanks!
jamessteed + 1 + 1 谢谢@Thanks!
decai + 1 + 1 谢谢@Thanks!
抱薪风雪雾 + 1 + 1 谢谢@Thanks!
Lee9527 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
dogox + 1 + 1 我很赞同!
xujie3517 + 1 我很赞同!
qinwang + 1 我很赞同!
HUAJIEN + 1 + 1 谢谢@Thanks!
ChenSSS + 1 + 1 谢谢@Thanks!
a446489393 + 1 + 1 我很赞同!
woxobo + 1 + 1 谢谢@Thanks!
茫茫狐 + 1 + 1 go开发的给你评个分,加油

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

yaozhongcan 发表于 2022-8-27 19:23
有个状态栏插件叫TrafficMonitor,可以直接在状态栏显示这一状态,我觉得更好用,有需要的人可以去GitHub下载

免费评分

参与人数 1热心值 +1 收起 理由
LeoHeisenberg + 1 谢谢@Thanks!

查看全部评分

Jakenb 发表于 2022-8-29 18:10
vsyour 发表于 2022-8-29 17:55
在cmd下执行, 给出具体报错信息可以帮你看看是哪里出了问题。
这种情况一般是配置问题。

在":"后面加了个空格就可以执行了
但是消息推送不到钉钉
config.yaml:

# 循环检测周期: 单位(秒)
checkTime: 60
# 使用率达到指定值发送通知
checkItems: {
  cpu: 80,
  mem: 40,
  disk: 90, #第1个分区,D盘.
}
# 钉钉配置信息
# 官方说明文档 https://open.dingtalk.com/document/robots/custom-robot-access/
dingding:  
  AccessToken: https://oapi.dingtalk.com/robot/send?access_token=********************************************
  # 加签
  Secret: SEC96**************************************************************
hjxhjxjx 发表于 2022-8-26 14:19
iawyxkdn8 发表于 2022-8-26 14:33
程序改变世界
qdsyok 发表于 2022-8-26 14:37
程序改变世界
azusys 发表于 2022-8-26 14:40
挺好的 可惜我单位不用钉钉了···
wangmin0001 发表于 2022-8-26 14:43
拿旧笔记本屏幕做了副屏,不过还是得谢谢楼主分享
ww5270616 发表于 2022-8-26 14:56
刚需啊,跑模型啥的需要
zxquan 发表于 2022-8-26 14:58
感谢支持,拿走
gdf2030 发表于 2022-8-26 15:03
监控电脑状态有一套这个足以了
kaci1129 发表于 2022-8-26 15:03
这绝对大神了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 提醒:禁止复制他人回复等『恶意灌水』行为,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-24 21:52

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表