小花狸監控之Redis
小花狸監控Redis和主機的狀態
監控主機狀態的程式碼
$GOPATH/src/probe/module/Host.go
package module
import (
"io/ioutil"
"net"
"os/exec"
"strconv"
"strings"
"syscall"
"time"
)
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 {
Ip string
Root Disk `json:root`
Data Disk `json:data`
Dbdata Disk `json:dbdata`
Memory
Cpu
Ts time.Time `json:ts`
}
func getHostAddr() (hostAddr string) {
addrs, _ := net.InterfaceAddrs()
for _, addr := range addrs {
ip := addr.String()
if !strings.HasPrefix(ip, "127") && !strings.HasPrefix(ip, "192") && !strings.HasSuffix(ip, "128") && !strings.HasSuffix(ip, "64") {
return ip[0:strings.LastIndex(ip, "/")]
}
}
return "127.0.0.1"
}
func (this *Host) HostUsage() {
this.Ip = getHostAddr()
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 NewHost(host *Host) {
host.Root = Disk{Name: "/"}
host.Data = Disk{Name: "/data"}
host.Dbdata = Disk{Name: "/dbdata"}
host.Ts = time.Now()
host.HostUsage()
}
監控Redis的程式碼
$GOLANG/src/probe/module/Redis.go
透過反射的方式,監控Redis資訊
package module
import (
"github.com/garyburd/redigo/redis"
"io/ioutil"
"os/exec"
"reflect"
"strconv"
"strings"
"time"
)
type RedisInfo struct {
Port string
Connected_clients int64
Blocked_clients int64
Used_memory int64
Role string
Instantaneous_ops_per_sec int64
Rejected_connections int64
Master_host string
Master_port int64
Master_link_status string
Master_last_io_seconds_ago int64
Connected_slaves int64
Cpu_ratio float64
Master0 string
Host Host
}
func GetRedisInfo(port string, password string) (redisinfo *RedisInfo) {
r := &RedisInfo{}
NewHost(&r.Host)
r.Port = port
ref := reflect.ValueOf(r)
conn, _ := redis.DialTimeout("tcp", ":"+port, 0, 1*time.Second, 1*time.Second)
conn.Do("AUTH", password)
defer conn.Close()
info, _ := redis.String(conn.Do("info"))
data := strings.Split(info, "\n")
for _, v := range data {
if !strings.HasPrefix(v, "#") && !strings.HasPrefix(v, "\r") && strings.Contains(v, ":") {
result := strings.Split(v, ":")
field := ref.Elem().FieldByName(strings.Title(result[0]))
if field.IsValid() {
switch field.Kind() {
case reflect.Int64:
val, _ := strconv.ParseInt(strings.Trim(result[1], "\r"), 10, 64)
field.SetInt(val)
case reflect.String:
field.SetString(strings.Trim(result[1], "\r"))
}
}
}
}
r.Cpu_ratio = getCpuInfo(port)
return r
}
func getCpuInfo(port string) float64 {
cmd := exec.Command("/bin/bash", "-c", `top -b -n 1 -p $(ps -ef | grep -w "redis-server\ \*:`+port+`" | awk '{print $2}') | awk '/redis-server/{print $9}' | sed 's/\r//g'`)
stdout, _ := cmd.StdoutPipe()
cmd.Start()
bytes, _ := ioutil.ReadAll(stdout)
cpuinfo, _ := strconv.ParseFloat(strings.Trim(string(bytes), "\n"), 64)
return cpuinfo
}
呼叫$GOPATH/src/main.go
package main
import (
"encoding/json"
"log"
"probe/module"
)
func main() {
r := module.GetRedisInfo("30000", "")
result, _ := json.Marshal(*r)
log.Println(string(result))
}
結果
{
"Port": "30000",
"Connected_clients": 3,
"Blocked_clients": 0,
"Used_memory": 852208,
"Role": "master",
"Instantaneous_ops_per_sec": 0,
"Rejected_connections": 0,
"Master_host": "",
"Master_port": 0,
"Master_link_status": "",
"Master_last_io_seconds_ago": 0,
"Connected_slaves": 0,
"Cpu_ratio": 0,
"Master0": "",
"Host": {
"Ip": "172.16.1.78",
"Root": {
"Name": "/",
"All": 50519138304,
"Used": 6745673728,
"Free": 41207181312
},
"Data": {
"Name": "/data",
"All": 50519138304,
"Used": 6745673728,
"Free": 41207181312
},
"Dbdata": {
"Name": "/dbdata",
"All": 0,
"Used": 0,
"Free": 0
},
"AllMemory": 7872,
"UsedMemory": 947,
"FreeMemory": 6925,
"CpuIdle": 99.9,
"Ts": "2015-03-19T19:08:23.780288023+08:00"
}
}
監控主機狀態的程式碼
$GOPATH/src/probe/module/Host.go
package module
import (
"io/ioutil"
"net"
"os/exec"
"strconv"
"strings"
"syscall"
"time"
)
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 {
Ip string
Root Disk `json:root`
Data Disk `json:data`
Dbdata Disk `json:dbdata`
Memory
Cpu
Ts time.Time `json:ts`
}
func getHostAddr() (hostAddr string) {
addrs, _ := net.InterfaceAddrs()
for _, addr := range addrs {
ip := addr.String()
if !strings.HasPrefix(ip, "127") && !strings.HasPrefix(ip, "192") && !strings.HasSuffix(ip, "128") && !strings.HasSuffix(ip, "64") {
return ip[0:strings.LastIndex(ip, "/")]
}
}
return "127.0.0.1"
}
func (this *Host) HostUsage() {
this.Ip = getHostAddr()
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 NewHost(host *Host) {
host.Root = Disk{Name: "/"}
host.Data = Disk{Name: "/data"}
host.Dbdata = Disk{Name: "/dbdata"}
host.Ts = time.Now()
host.HostUsage()
}
監控Redis的程式碼
$GOLANG/src/probe/module/Redis.go
透過反射的方式,監控Redis資訊
package module
import (
"github.com/garyburd/redigo/redis"
"io/ioutil"
"os/exec"
"reflect"
"strconv"
"strings"
"time"
)
type RedisInfo struct {
Port string
Connected_clients int64
Blocked_clients int64
Used_memory int64
Role string
Instantaneous_ops_per_sec int64
Rejected_connections int64
Master_host string
Master_port int64
Master_link_status string
Master_last_io_seconds_ago int64
Connected_slaves int64
Cpu_ratio float64
Master0 string
Host Host
}
func GetRedisInfo(port string, password string) (redisinfo *RedisInfo) {
r := &RedisInfo{}
NewHost(&r.Host)
r.Port = port
ref := reflect.ValueOf(r)
conn, _ := redis.DialTimeout("tcp", ":"+port, 0, 1*time.Second, 1*time.Second)
conn.Do("AUTH", password)
defer conn.Close()
info, _ := redis.String(conn.Do("info"))
data := strings.Split(info, "\n")
for _, v := range data {
if !strings.HasPrefix(v, "#") && !strings.HasPrefix(v, "\r") && strings.Contains(v, ":") {
result := strings.Split(v, ":")
field := ref.Elem().FieldByName(strings.Title(result[0]))
if field.IsValid() {
switch field.Kind() {
case reflect.Int64:
val, _ := strconv.ParseInt(strings.Trim(result[1], "\r"), 10, 64)
field.SetInt(val)
case reflect.String:
field.SetString(strings.Trim(result[1], "\r"))
}
}
}
}
r.Cpu_ratio = getCpuInfo(port)
return r
}
func getCpuInfo(port string) float64 {
cmd := exec.Command("/bin/bash", "-c", `top -b -n 1 -p $(ps -ef | grep -w "redis-server\ \*:`+port+`" | awk '{print $2}') | awk '/redis-server/{print $9}' | sed 's/\r//g'`)
stdout, _ := cmd.StdoutPipe()
cmd.Start()
bytes, _ := ioutil.ReadAll(stdout)
cpuinfo, _ := strconv.ParseFloat(strings.Trim(string(bytes), "\n"), 64)
return cpuinfo
}
呼叫$GOPATH/src/main.go
package main
import (
"encoding/json"
"log"
"probe/module"
)
func main() {
r := module.GetRedisInfo("30000", "")
result, _ := json.Marshal(*r)
log.Println(string(result))
}
結果
{
"Port": "30000",
"Connected_clients": 3,
"Blocked_clients": 0,
"Used_memory": 852208,
"Role": "master",
"Instantaneous_ops_per_sec": 0,
"Rejected_connections": 0,
"Master_host": "",
"Master_port": 0,
"Master_link_status": "",
"Master_last_io_seconds_ago": 0,
"Connected_slaves": 0,
"Cpu_ratio": 0,
"Master0": "",
"Host": {
"Ip": "172.16.1.78",
"Root": {
"Name": "/",
"All": 50519138304,
"Used": 6745673728,
"Free": 41207181312
},
"Data": {
"Name": "/data",
"All": 50519138304,
"Used": 6745673728,
"Free": 41207181312
},
"Dbdata": {
"Name": "/dbdata",
"All": 0,
"Used": 0,
"Free": 0
},
"AllMemory": 7872,
"UsedMemory": 947,
"FreeMemory": 6925,
"CpuIdle": 99.9,
"Ts": "2015-03-19T19:08:23.780288023+08:00"
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1466480/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 小花狸監控之MySQLMySql
- 小花狸監控之加密加密
- 小花狸監控之MongodbMongoDB
- 小花狸監控之網路收發
- 小花狸監控之安全加固expect備份指令碼指令碼
- 小花狸ITPUB部落格備份工具
- redis監控之Redis LiveRedis
- redis監控工具之redis-liveRedis
- Redis監控Redis
- Redis監控工具Redis
- Redis監控方案Redis
- 【Redis】redis阻塞監控項分析Redis
- TenSunS監控REDIS:使用一個redis_exporter監控所有的Redis例項RedisExport
- 監控 redis 執行命令Redis
- Redis Manager 如何檢視監控Redis
- zabbix監控單例項redis單例Redis
- redis sentinel 叢集監控 配置Redis
- MongoDB之監控MongoDB
- 使用夜鶯+categraf監控redis和redis叢集Redis
- 伺服器監控之 ping 監控伺服器
- redis效能監控工具redis-stat安裝初探Redis
- Redis服務監控之RedisLive安裝部署(親測可用)Redis
- Redis安裝+叢集+效能監控Redis
- Redis 常用監控資訊命令總結Redis
- 實時監控redis -- redislive與redis踩坑配置Redis
- 【Redis實戰專題】「效能監控系列」全方位探索Redis的效能監控以及最佳化指南Redis
- redis3.0叢集監控指令碼RedisS3指令碼
- 11.prometheus監控之黑盒(blackbox)監控Prometheus
- ORACLE監控之OSW部署Oracle
- MySQL監控工具之orzdbaMySql
- Apache Kafka監控之KafkaOffsetMonitorApacheKafka
- 【Redis 系列】redis 學習六,redis 事務處理和監控事務Redis
- python3.10監控redis例項PythonRedis
- 效能測試之Docker監控Docker
- Zabbix監控之遷移Zabbix
- PostgreSQL之鎖監控指令碼SQL指令碼
- springboot之actuator監控Spring Boot
- 監控mysql之orzdba和orztopMySql