System Inspection Application (i)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

System inspection Applications (1)

Test environment:

    • ubuntu16.04
    • golang:go1.8

Content

Getenvfunc (): Get environment variable function
Getenvsvr (): Currently only obtains CPU cores

Code

  Package Mainimport ("bytes" "detect_sys/ming" "FMT" "OS" "os/exec"/* "StrConv" */"s Trings ")//OSENV system environment variable gets the type osenv struct {envuser string Envhome string Pid int}func main () {FMT. Println ("Start") fmt. Println (Getenvfunc ()) Getenvsvr () Ming. Strfunc () fmt. Println ("End")}//getenvfunc gets the environment variable function func getenvfunc () (res osenv) {Res. Envuser = OS. Getenv ("USER") Res. envhome = OS. Getenv ("HOME") Res. Pid = OS. Getpid () return res}//Getenvsvr 1. Gets the number of CPU cores func getenvsvr () int {cmd: = exec. Command ("/bin/bash", "-C", "Lscpu |grep ' CPU (s) ' |grep-v ' li ' |grep-v '-' |awk ' {print '} '") var out bytes. Buffer cmd. Stdout = &out Err: = cmd. Run () if err! = Nil {fmt. PRINTLN ("Error") return-1}//Remove space str: = strings. Replace (out. String (), "", "",-1)//remove line break str = strings. Replace (str, "\ n", "",-1) fmt. PRINTLN ("CPU Cores:" + str) return 1}  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.