ITPub博客

首页 > 应用开发 > IT综合 > 小花狸监控之主机状态

小花狸监控之主机状态

原创 IT综合 作者:壹頁書 时间:2015-03-18 12:40:35 0 删除 编辑
小花狸监控,使用Golang编写
预计监控内容
1.主机CPU,内存,磁盘使用率
2.Redis
3.Mongodb
4.MySQL
5.Oracle
6.LVS(*)
7.KeepAlived(*)

图难于其易,为大于其细
下面是使用Golang获取主机资源使用率
Golang语言非常简单直观.和使用命令直接获取几乎无异.

package main

import (
    "encoding/json"
    "io/ioutil"
    "log"
    "os/exec"
    "strconv"
    "strings"
    "syscall"
)

type Disk struct {
    Name string `json:name`
    All  uint64 `json:all`
    Used uint64 `json:used`
    Free uint64 `json:free`
}

func (this *Disk) DiskUsage() {
    fs := syscall.Statfs_t{}
    err := syscall.Statfs(this.Name, &fs)
    if err != nil {
        this.All = 0
        this.Free = 0
        this.Used = 0
    }
    this.All = fs.Blocks * uint64(fs.Bsize)
    this.Free = fs.Bavail * uint64(fs.Bsize)
    this.Used = this.All - fs.Bfree*uint64(fs.Bsize)
}

type Memory struct {
    AllMemory  uint64 `json:allmemory`
    UsedMemory uint64 `json:usedmemory`
    FreeMemory uint64 `json:freememory`
}

func (this *Memory) MemoryUsage() {
    cmd := exec.Command("/bin/bash", "-c", `free -m | awk 'NR==3 {print $3"TAB"$4}'`)
    stdout, _ := cmd.StdoutPipe()
    cmd.Start()
    bytes, _ := ioutil.ReadAll(stdout)
    data := strings.Split(string(bytes), "TAB")
    this.UsedMemory, _ = strconv.ParseUint(data[0], 10, 64)
    this.FreeMemory, _ = strconv.ParseUint(strings.Trim(data[1], "\n"), 10, 64)
    this.AllMemory = this.UsedMemory + this.FreeMemory
}

type Host struct {    
    Root   Disk `json:root`
    Data   Disk `json:data`
    Dbdata Disk `json:dbdata`
    Memory
    Cpu
}

func (this *Host) HostUsage() {
    this.MemoryUsage()
    this.Root.DiskUsage()
    this.Data.DiskUsage()
    this.Dbdata.DiskUsage()
    this.Cpu.CpuUsage()
}

type Cpu struct {
    CpuIdle float64
}


func (this *Cpu) CpuUsage() {
    cmd := exec.Command("/bin/bash", "-c", `top -b -n 1 | grep Cpu | awk -F"," '{print $4}' | sed 's/%id//g;s/^ //g'`)
    stdout, _ := cmd.StdoutPipe()
    cmd.Start()
    bytes, _ := ioutil.ReadAll(stdout)
    cpuidle, _ := strconv.ParseFloat(strings.Trim(string(bytes), "\n"), 64)
    this.CpuIdle = cpuidle
}

func main() {
    log.SetFlags(log.Lshortfile | log.LstdFlags)    
    host := &Host{
        Root:   Disk{Name: "/"},
        Data:   Disk{Name: "/data"},
        Dbdata: Disk{Name: "/dbdata"}}
    host.HostUsage()
    result, _ := json.Marshal(host)
    log.Println(string(result))
}

程序输出结果:
{
    "Root": {
        "Name": "/", 
        "All": 50519138304, 
        "Used": 6723317760, 
        "Free": 41229537280
    }, 
    "Data": {
        "Name": "/data", 
        "All": 50519138304, 
        "Used": 6723317760, 
        "Free": 41229537280
    }, 
    "Dbdata": {
        "Name": "/dbdata", 
        "All": 0, 
        "Used": 0, 
        "Free": 0
    }, 
    "AllMemory": 7872, 
    "UsedMemory": 916, 
    "FreeMemory": 6956, 
    "CpuIdle": 99.9
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1464317/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2013-10-19

  • 博文量
    620
  • 访问量
    6339997