GithubHelp home page GithubHelp logo

goprocinfo's Introduction

goprocinfo

/proc information parser for Go.

Usage

import (
	"log"

	linuxproc "github.com/c9s/goprocinfo/linux"
)

stat, err := linuxproc.ReadStat("/proc/stat")
if err != nil {
	log.Fatal("stat read fail")
}

for _, s := range stat.CPUStats {
	// s.User
	// s.Nice
	// s.System
	// s.Idle
	// s.IOWait
}

// stat.CPUStatAll
// stat.CPUStats
// stat.Processes
// stat.BootTime
// ... etc

Documentation

Full documentation is available at Godoc.

Reference

License

goprocinfo is distributed under the MIT license.

goprocinfo's People

Contributors

c9s avatar codelingobot avatar cskksc avatar dream0411 avatar faeriol avatar gregcope avatar hilyjiang avatar josegonzalez avatar joshgarnett avatar lespea avatar lzap avatar mislink avatar moooofly avatar nicolascb avatar pariviere avatar penberg avatar q3k avatar rdwilliamson avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

goprocinfo's Issues

Out of index crash

Hi, I am running this on CentOS 6.2, kernel 2.6.32-220.23.1.el6.x86_64. When attempting to call ReadStat, I run into this:

panic: runtime error: index out of range

goroutine 24 [running]:
runtime.panic(0x757160, 0xba53bc)
    /usr/local/go/src/pkg/runtime/panic.c:279 +0xf5
github.com/c9s/goprocinfo/linux.createCPUStat(0xc20803ebe0, 0xa, 0xa, 0x3)
    /home/crawler/gopath/src/github.com/c9s/goprocinfo/linux/stat.go:46 +0x386
github.com/c9s/goprocinfo/linux.ReadStat(0x7b9bb0, 0xa, 0x0, 0x0, 0x0)
    /home/crawler/gopath/src/github.com/c9s/goprocinfo/linux/stat.go:66 +0x32d
my functions

Perhaps you did not mean to one index that function?

Get all processes info build error

func main() {
stat, err := linuxproc.ReadStat("/proc/stat")
Checkerr(err)
for _, s := range stat.Processes {
fmt.Println(s.Status.pid, s.Status.name, s.Cmdline)
}
}

cannot range over stat.Processes (type uint64)

support for unix sockets

it appears support for unix socket is missing. Is there functionality to read unix sockets?

ie, /proc/net/unix ?

Index out of range on a pid

Hi, thx for this api, that's very helpful!

But I get an error when I try to iterate over all PIDs.

max, e := linux.ReadMaxPID("/proc/sys/kernel/pid_max")
if e != nil {
    log.Fatal(e)
}
l, e := linux.ListPID("/proc", max)
if e != nil {
    log.Fatal(e)
}
for _, pid := range l {
    proc, e := linux.ReadProcess(pid, "/proc/")
    if e != nil {
        fmt.Println(e)
    } else {
        fmt.Println(proc.Cmdline)
    }
}
[...]
gdm-session-worker [pam/gdm-password]
4853
/usr/lib/systemd/systemd --user
panic: runtime error: index out of range

goroutine 1 [running]:
github.com/c9s/goprocinfo/linux.ReadProcessStat(0xc2080882a0, 0xf, 0x2, 0x0, 0x0)
    /home/ghigt/go/src/github.com/c9s/goprocinfo/linux/process_stat.go:86 +0x298a
github.com/c9s/goprocinfo/linux.ReadProcess(0x12f6, 0x54c0f0, 0x6, 0x20, 0x0, 0x0)
    /home/ghigt/go/src/github.com/c9s/goprocinfo/linux/process.go:39 +0x396
main.main()
    /home/ghigt/test.go:28 +0x2c9
exit status 2

ipv6 address handling in ReadNetTCPSockets

ReadNetTCPSockets with ipv6 returns incorrectly formatted addresses in LocalAddress. NetTCPSocket embeds NetSocket which is defines LocalAddress as a string (https://godoc.org/github.com/c9s/goprocinfo/linux#NetSocket )

For a IPv6 address the correct way to format an address is to use square brackets for the host portion. This allows the address to be properly parsed by net.SplitHostPort. I see ":::80" being returned when it should be "[::]:80"

Is this intended behavior?

[Question] MemInfo

Hi, I have some questions about MemInfo (linux/meminfo.go), and I hope you could answer them.

Originally, it was a struct but you changed it to a map : why ?
Do you need a contributor to implement this struct (since its the only "module" to store its data as a map) ? Or do you want to keep it as is ?

Thank you in advance.

Include CpusAllowedList for better user experience

In the process_status.go file, CpusAllowed is given as an array of uint32 representing the mask format. This requires the user to implement the logic to translate this to meaningful core numbers. The status file also provides a field called Cpus_allowed_list, which gives the cores in a list format. In that spirit I propose these additions:

Note: I'm intentionally not touching the format of CpusAllowed in order to ensure backwards compatibility.

// New type for list format
type ListFormat []uint32

// Add new field to ProcessStatus struct
type ProcessStatus struct {
...
    CpusAllowedList ListFormat
}

// Parsing code
// Decode the listformat of CpusAllowedList into a slice of numbers representing one core each
// eg ParsedCpusAllowedList = "1-3"  -> CpusAllowedList = [1,2,3]

// The user can print the CpusAllowedList in canonical list form 
// by calling the implemented String() method
// eg cpusAllowedList.String() = "1-3"
func (c ListFormat) String() string {
   ...
}

// The user can easily find out the number of cores the 
// application is allowed to run on by calling:
cores := len(cpusAllowedList)

All in all, this enables users to have easy access to what cores are allowed as well as the number of cores allowed. The same can be done for MemsAllowed. Reference man page.

I'm willing to implement this but I want to see if there is an objection about the design/feature before I start!

License

Hello,

what is the license of the project? Would you mind adding license file?

I'd like to provide /proc/mounts parsing, would you accept the patch?

LZ

Some problem of ListPID

Hello, I found that the 'ListPID()' function will show all of processes include sub-threads. And this may not suit for someone. I found source code in 'top' command, it only read dir in /proc which is digit & isDir, this will not list the pid which is not main thread. I give my simple code under below:

func ListProgram(path string) ([]uint64, error) {
        pids := make([]uint64, 0, 20)
	infos, err := ioutil.ReadDir(path)
	if err != nil {
		return  nil, err
	}
	for _, info := range(infos) {
		pid, err := strconv.ParseUint(info.Name(), 10, 64)
		if err != nil {
			continue
		}
		pids = append(pids, pid)
	}
	return pids, nil
}

Calculate cpu usage % of a process

Is there a more precise (better) way to do it than that?

pid, _ := strconv.ParseUint(os.Args[1], 10, 64)

for {
    p1, _ := linux.ReadProcess(pid, "/proc")

    time.Sleep(time.Second)

    p2, _ := linux.ReadProcess(pid, "/proc")

    user := int64(p2.Stat.Utime) + p2.Stat.Cutime - int64(p1.Stat.Utime) + p1.Stat.Cutime
    syst := int64(p2.Stat.Stime) + p2.Stat.Cstime - int64(p1.Stat.Stime) + p1.Stat.Cstime

    fmt.Printf("%v: %v  \r", pid, user+syst)
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.