本文整理匯總了Golang中github.com/shirou/gopsutil/host.HostInfo函數的典型用法代碼示例。如果您正苦於以下問題:Golang HostInfo函數的具體用法?Golang HostInfo怎麽用?Golang HostInfo使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了HostInfo函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: osName
// osName returns the name of the OS.
func osName() string {
h, err := host.HostInfo()
if err != nil || h.OS == "" {
return "Linux"
}
return h.OS
}
示例2: main
func main() {
v, _ := mem.VirtualMemory()
// almost every return value is a struct
fmt.Printf("Total: %v, Free:%v, UsedPercent:%f%%\n", v.Total, v.Free, v.UsedPercent)
// convert to JSON. String() is also implemented
fmt.Println(v)
h, err := host.HostInfo()
if err != nil {
fmt.Println("err:", err)
} else {
fmt.Printf("hostname %v", h)
}
c, err := cpu.CPUInfo()
if err != nil {
fmt.Println("err:", err)
}
for _, v := range c {
fmt.Printf("cpu info %v \n ", v)
}
}
示例3: Gather
func (_ *SystemStats) Gather(acc telegraf.Accumulator) error {
loadavg, err := load.LoadAvg()
if err != nil {
return err
}
hostinfo, err := host.HostInfo()
if err != nil {
return err
}
users, err := host.Users()
if err != nil {
return err
}
fields := map[string]interface{}{
"load1": loadavg.Load1,
"load5": loadavg.Load5,
"load15": loadavg.Load15,
"uptime": hostinfo.Uptime,
"n_users": len(users),
"uptime_format": format_uptime(hostinfo.Uptime),
"n_cpus": runtime.NumCPU(),
}
acc.AddFields("system", fields, nil)
return nil
}
示例4: Fingerprint
func (f *HostFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
hostInfo, err := host.HostInfo()
if err != nil {
f.logger.Println("[WARN] Error retrieving host information: ", err)
return false, err
}
node.Attributes["os.name"] = hostInfo.Platform
node.Attributes["os.version"] = hostInfo.PlatformVersion
node.Attributes["kernel.name"] = runtime.GOOS
node.Attributes["kernel.version"] = ""
if runtime.GOOS != "windows" {
out, err := exec.Command("uname", "-r").Output()
if err != nil {
return false, fmt.Errorf("Failed to run uname: %s", err)
}
node.Attributes["kernel.version"] = strings.Trim(string(out), "\n")
}
node.Attributes["hostname"] = hostInfo.Hostname
return true, nil
}
示例5: osVersion
// osVersion returns the OS version.
func osVersion() string {
h, err := host.HostInfo()
if err != nil || h.PlatformVersion == "" {
return "0.0"
}
return h.PlatformVersion
}
示例6: hostInfo
func hostInfo(ctx *gin.Context) {
// For static
info, err := host.HostInfo()
if err != nil {
ctx.AbortWithError(http.StatusInternalServerError, err)
return
}
ctx.JSON(http.StatusOK, info)
}
示例7: handleHostCommand
func handleHostCommand(w http.ResponseWriter) {
var buffer bytes.Buffer
info, _ := host.HostInfo()
buffer.WriteString(fmt.Sprintf("Boottime: %v\n", info.BootTime))
buffer.WriteString(fmt.Sprintf("Hostname: %v\n", info.Hostname))
buffer.WriteString(fmt.Sprintf("Uptime: %v\n", info.Uptime))
io.WriteString(w, buffer.String())
}
示例8: Distro
// Distro - gets distro info
// {'version': '14.04', 'name': 'Ubuntu'}
func Distro() DistroStruct {
host, _ := pshost.HostInfo()
d := DistroStruct{
Version: host.PlatformVersion,
Name: host.Platform,
}
return d
}
示例9: NewTextDetail
func NewTextDetail(alert string) (*TextDetail, error) {
hostinfo, err := host.HostInfo()
if err != nil {
return nil, err
}
return &TextDetail{
Alert: alert,
HostName: hostinfo.Hostname,
}, nil
}
示例10: main
func main() {
uname, err := getUname()
if err != nil {
fmt.Println("error: ", err)
}
fmt.Println("uname: ", uname)
hostinfo := host.HostInfo()
fmt.Println(hostinfo.String())
}
示例11: ajax
func ajax(c web.C, w http.ResponseWriter, r *http.Request) {
sys, _ := host.HostInfo()
action := r.FormValue("action")
if action == "time" {
now := time.Now()
past := now.Add(-1 * time.Duration(sys.Uptime) * time.Second)
fmt.Fprintf(w, "%s;%s;%s", now.Format("January 2, 2006"), now.Format("3:04 pm"), past.Format("January 2, 2006"))
}
if action == "uptime" {
uptimeDuration := time.Duration(sys.Uptime) * time.Second
days := int(uptimeDuration.Hours() / 24)
hours := int(uptimeDuration.Hours()) % 24
minutes := int(uptimeDuration.Minutes()) % 60
fmt.Fprintf(w, "%d;%d;%d", days, hours, minutes)
}
if action == "image" {
bingResponse, err := http.Get("http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US")
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer bingResponse.Body.Close()
contents, err := ioutil.ReadAll(bingResponse.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
var animals BingImage
err = json.Unmarshal(contents, &animals)
if err != nil {
fmt.Println("error:", err)
}
fmt.Fprintf(w, "%s;%s", "https://www.bing.com/"+animals.Images[0].URL, animals.Images[0].Copyright)
}
}
if action == "location" {
ipResponse, err := http.Get("http://icanhazip.com")
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
} else {
defer ipResponse.Body.Close()
contents, err := ioutil.ReadAll(ipResponse.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
fmt.Fprintf(w, "%s", string(contents))
}
}
}
示例12: initHostInfo
func initHostInfo(monitorData *monitoringData) error {
hostInfo, err := host.HostInfo()
if hostInfo != nil {
monitorData.ComputerName = hostInfo.Hostname
monitorData.OperatingSystem = hostInfo.OS
monitorData.Platform = hostInfo.Platform
monitorData.PlatformFamily = hostInfo.PlatformFamily
monitorData.ProcessNumber = hostInfo.Procs
}
return err
}
示例13: home
func home(c web.C, w http.ResponseWriter, r *http.Request) {
p := &Page{Title: "Uptimey"}
sys, _ := host.HostInfo()
now := time.Now()
past := now.Add(-1 * time.Duration(sys.Uptime) * time.Second)
p.StartDate = past
tmpl, err := FSString(false, "/assets/index.html")
if err != nil {
fmt.Println(err.Error())
}
t, _ := template.New("index").Parse(tmpl)
t.Execute(w, p)
}
示例14: printStatus
func printStatus() {
h, err := host.HostInfo()
if err != nil {
log.Println("Could not get host info", err)
}
log.Println(h.String())
procs, err := ps.Processes()
if err != nil {
log.Println("Could not get processes")
}
log.Println(len(procs))
for _, e := range procs {
log.Println(e.Executable(), e.Pid(), e.PPid())
}
}
示例15: Gather
func (_ *SystemStats) Gather(acc plugins.Accumulator) error {
loadavg, err := load.LoadAvg()
if err != nil {
return err
}
hostinfo, err := host.HostInfo()
if err != nil {
return err
}
acc.Add("load1", loadavg.Load1, nil)
acc.Add("load5", loadavg.Load5, nil)
acc.Add("load15", loadavg.Load15, nil)
acc.Add("uptime", hostinfo.Uptime, nil)
acc.Add("uptime_format", format_uptime(hostinfo.Uptime), nil)
return nil
}