• 磁盘监控脚本-go语言


    基于go语言写了一个简单的磁盘监控脚本,兼容linux和windows,

    具体实现代码如下:

    1. package main
    2. import (
    3. "database/sql"
    4. "fmt"
    5. _ "github.com/go-sql-driver/mysql"
    6. "github.com/shirou/gopsutil/v3/disk"
    7. "log"
    8. Lnet "net"
    9. "os"
    10. "strconv"
    11. "strings"
    12. "time"
    13. )
    14. var MysqlDB *sql.DB
    15. var MysqlErr error
    16. var dbDsn string
    17. var dbSourceInfo string
    18. func Initdb(){
    19. os.Setenv("NLS_LANG", "SIMPLIFIED CHINESE_CHINA.AL32UTF8")
    20. user:="xxxx"
    21. passwd:="xxxx"
    22. hostIp:="xxxx"
    23. port:="xxxx"
    24. dbname:="xxxx"
    25. charset:="utf8mb4"
    26. dbDsn:=fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&timeout=5s&readTimeout=5s&writeTimeout=5s", user, passwd, hostIp, port, dbname, charset)
    27. dbSourceInfo =fmt.Sprintf("%s:%s/%s",hostIp, port, dbname)
    28. //log.Println(dbDsn)
    29. //log.Println(dbSourceInfo)
    30. //打开连接
    31. MysqlDB,MysqlErr= sql.Open("mysql",dbDsn)
    32. //log.Println(MysqlErr)
    33. //checkErr(dbDsn,1,1,MysqlErr)
    34. //log.Println(MysqlDB)
    35. //最大连接
    36. MysqlDB.SetMaxOpenConns(2)
    37. //闲置空闲连接
    38. MysqlDB.SetMaxIdleConns(2)
    39. //最大连接周期
    40. MysqlDB.SetConnMaxLifetime(10)
    41. if MysqlErr = MysqlDB.Ping(); nil != MysqlErr {
    42. os.Exit(1)
    43. //checkErr(dbDsn,1,1,MysqlErr)
    44. }
    45. }
    46. func getDiskInfo() {
    47. parts, _ := disk.Partitions(true)
    48. diskTime := time.Now().Format("2006-01-02 15:04:05")
    49. localIp:=GetOutboundIP()
    50. disks := ""
    51. for _, part := range parts {
    52. fsType := part.Fstype
    53. if fsType == "xfs" || fsType == "ext2" || fsType == "ext3" || fsType == "ext4" || fsType == "nfs" || fsType == "NTFS" || fsType == "nfs4" || fsType == "NTFS4" ||fsType == "cifs" {
    54. diskInfo, _ := disk.Usage(part.Mountpoint)
    55. disks += part.Mountpoint + ","
    56. diskPct, _ := strconv.ParseFloat(fmt.Sprintf("%.2f", diskInfo.UsedPercent), 64)
    57. //checkErr(ins.fIp,4,1,errPct)
    58. DiskAlarm := Query(MysqlDB, dbDsn, 1, 1, " select f_alarm_disk as value from t_options order by f_update_time desc limit 1")[0]["value"]
    59. fDiskAlarm, _ := strconv.ParseFloat(DiskAlarm, 64)
    60. if fDiskAlarm == 0 {
    61. fDiskAlarm = 90
    62. }
    63. log.Printf(part.Mountpoint)
    64. if strings.Contains(part.Mountpoint, "archivelog") {
    65. if diskPct>=50{
    66. msg := fmt.Sprintf("%v used space is higher last_value:%v%%", part.Mountpoint, diskPct)
    67. msgError := fmt.Sprintf("Problem!! %s %s %s", localIp, msg, diskTime)
    68. //sendSms(toAddress,msgError,"LTAIiGjULxYERylh","YIIGlydOAUyZVEIjvvAA33WFCCko5G",
    69. // "智创博为","SMS_219743608","name")
    70. inSql := fmt.Sprintf("insert into t_alarm_send_info(f_send_type,f_send_level,f_send_info) values(%v,%v,'%v')", 3, 'C', msgError)
    71. //log.Println(inSql)
    72. Exec(MysqlDB, dbSourceInfo, 1, 1, inSql)
    73. }
    74. } else {
    75. if diskPct > fDiskAlarm {
    76. msg := fmt.Sprintf("%v used space is higher last_value:%v%%", part.Mountpoint, diskPct)
    77. msgError := fmt.Sprintf("Problem!! %s %s %s", localIp, msg, diskTime)
    78. //sendSms(toAddress,msgError,"LTAIiGjULxYERylh","YIIGlydOAUyZVEIjvvAA33WFCCko5G",
    79. // "智创博为","SMS_219743608","name")
    80. inSql := fmt.Sprintf("insert into t_alarm_send_info(f_send_type,f_send_level,f_send_info) values(%v,%v,'%v')", 3, 'C', msgError)
    81. //log.Println(inSql)
    82. Exec(MysqlDB, dbSourceInfo, 1, 1, inSql)
    83. }
    84. }
    85. }
    86. }
    87. }
    88. //func sendSms(to string,msg string,AccessKeyId string,AccessKeySecret string,SignName string,TemplateCode string,TemplateParam string) {
    89. // client, _ := dysmsapi.NewClientWithAccessKey("cn-hangzhou", AccessKeyId, AccessKeySecret)
    90. // request := dysmsapi.CreateSendSmsRequest()
    91. // request.Scheme = "https"
    92. // msg = strings.Replace(msg, "\"", "", -1)
    93. // request.PhoneNumbers = to
    94. // request.SignName = SignName
    95. // request.TemplateCode = TemplateCode
    96. // request.TemplateParam = "{\"" + TemplateParam + "\":\"" + msg + "\"}"
    97. //
    98. // response, _ := client.SendSms(request)
    99. // log.Println(errors.New(response.Message).Error())
    100. //
    101. //}
    102. func Exec(db *sql.DB,dbInfo string,types int ,level int,query string){
    103. _,err:=db.Exec(query)
    104. log.Println(err)
    105. }
    106. func Query(db *sql.DB,dbinfo string,types int ,level int,query string)(result map[int]map[string]string) {
    107. rows,_ := db.Query(query)
    108. defer rows.Close()
    109. cols,_ :=rows.Columns() //返回所有列
    110. vals :=make([][]byte,len(cols)) //这里表示一行所有列的值,用[]byte表示
    111. scans :=make([]interface{},len(cols)) //这里表示一行填充数据
    112. for k,_:=range vals{
    113. scans[k] = &vals[k]
    114. }
    115. i :=0
    116. result = make(map[int]map[string]string)
    117. for rows.Next(){
    118. rows.Scan(scans...) //填充数据
    119. row :=make(map[string]string) //每行数据
    120. for k,v := range vals { //把vals中的数据复制到row中
    121. key :=cols[k]
    122. //fmt.Println(string(v)) //这里把[]byte数据转成string
    123. row[key] = string(v)
    124. }
    125. result[i] = row
    126. i++
    127. }
    128. rows.Close()
    129. return result
    130. }
    131. func GetOutboundIP() string {
    132. conn, err := Lnet.Dial("udp", "8.8.8.8:80")
    133. if err != nil {
    134. //dbLog("自动获取ip失败",1,1,err.Error())
    135. panic(err)
    136. }
    137. defer conn.Close()
    138. localAddr := conn.LocalAddr().(*Lnet.UDPAddr)
    139. return localAddr.IP.String()
    140. }
    141. func main(){
    142. Initdb()
    143. getDiskInfo()
    144. }

  • 相关阅读:
    Netty-编码和解码
    高光谱成像技术简介,怎么选择成像方案?
    轻量容器引擎Docker基础使用
    进阶C++__STL__stack和queue | deque与priority queue
    jvm 内存区域划分-CSDN博客
    tlaplus-vscode插件使用记录
    C++ STL进阶与补充(list容器)
    Day 63 双向循环链表
    Github 2024-02-13 开源项目日报 Top9
    现货黄金与黄金一样吗?
  • 原文地址:https://blog.csdn.net/shiyu1157758655/article/details/134457537