Beispiel #1
0
func Info() (*InfoStat, error) {
	ret := &InfoStat{
		OS: runtime.GOOS,
	}

	hostname, err := os.Hostname()
	if err == nil {
		ret.Hostname = hostname
	}

	platform, family, version, err := PlatformInformation()
	if err == nil {
		ret.Platform = platform
		ret.PlatformFamily = family
		ret.PlatformVersion = version
	} else {
		return ret, err
	}

	boot, err := BootTime()
	if err == nil {
		ret.BootTime = boot
		ret.Uptime, _ = Uptime()
	}

	procs, err := process.Pids()
	if err == nil {
		ret.Procs = uint64(len(procs))
	}

	return ret, nil
}
Beispiel #2
0
func HostInfo() (*HostInfoStat, error) {
	ret := &HostInfoStat{
		OS: runtime.GOOS,
	}

	hostname, err := os.Hostname()
	if err == nil {
		ret.Hostname = hostname
	}

	platform, family, version, err := GetPlatformInformation()
	if err == nil {
		ret.Platform = platform
		ret.PlatformFamily = family
		ret.PlatformVersion = version
	}

	ret.Uptime, err = BootTime()
	if err != nil {
		return ret, err
	}

	procs, err := process.Pids()
	if err != nil {
		return ret, err
	}

	ret.Procs = uint64(len(procs))

	return ret, nil
}
Beispiel #3
0
func ProcessTable() map[int32]string {
	res := make(map[int32]string)
	pids, _ := process.Pids()

	for i := 0; i < len(pids); i++ {
		pid := pids[i]
		proc, _ := process.NewProcess(pid)
		cline, _ := proc.Cmdline()
		res[pid] = cline
	}
	return res
}
Beispiel #4
0
func Info() (*InfoStat, error) {
	ret := &InfoStat{
		OS:             runtime.GOOS,
		PlatformFamily: "darwin",
	}

	hostname, err := os.Hostname()
	if err == nil {
		ret.Hostname = hostname
	}

	platform, family, version, err := PlatformInformation()
	if err == nil {
		ret.Platform = platform
		ret.PlatformFamily = family
		ret.PlatformVersion = version
		ret.KernelVersion = version
	}

	system, role, err := Virtualization()
	if err == nil {
		ret.VirtualizationSystem = system
		ret.VirtualizationRole = role
	}

	boot, err := BootTime()
	if err == nil {
		ret.BootTime = boot
		ret.Uptime = uptime(boot)
	}

	procs, err := process.Pids()
	if err == nil {
		ret.Procs = uint64(len(procs))
	}

	values, err := common.DoSysctrl("kern.uuid")
	if err == nil && len(values) == 1 && values[0] != "" {
		ret.HostID = values[0]
	}

	return ret, nil
}
Beispiel #5
0
func HostInfo() (*HostInfoStat, error) {
	ret := &HostInfoStat{}
	hostname, err := os.Hostname()
	if err != nil {
		return ret, err
	}

	ret.Hostname = hostname
	uptime, err := BootTime()
	if err == nil {
		ret.Uptime = uptime
	}

	procs, err := process.Pids()
	if err != nil {
		return ret, err
	}

	ret.Procs = uint64(len(procs))

	return ret, nil
}
Beispiel #6
0
func Info() (*InfoStat, error) {
	ret := &InfoStat{
		OS:             runtime.GOOS,
		PlatformFamily: "darwin",
	}

	hostname, err := os.Hostname()
	if err == nil {
		ret.Hostname = hostname
	}

	platform, family, version, err := PlatformInformation()
	if err == nil {
		ret.Platform = platform
		ret.PlatformFamily = family
		ret.PlatformVersion = version
	}

	system, role, err := Virtualization()
	if err == nil {
		ret.VirtualizationSystem = system
		ret.VirtualizationRole = role
	}

	boot, err := BootTime()
	if err == nil {
		ret.BootTime = boot
		ret.Uptime = uptime(boot)
	}

	procs, err := process.Pids()
	if err == nil {
		ret.Procs = uint64(len(procs))
	}

	return ret, nil
}
Beispiel #7
0
)

var root = hateoas.NewResource(
	hateoas.Path("/"),
	hateoas.AddLink("cpus", cpuRsc),
	hateoas.AddLink("load", loadRsc),
	hateoas.AddLink("processes", procRsc),
	hateoas.HEAD(mohttp.EmptyBodyHandler),
)

var cpuRsc = hateoas.NewResource(
	hateoas.Path("/cpus"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return cpu.CPUInfo()
	})),
)

var loadRsc = hateoas.NewResource(
	hateoas.Path("/load"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return load.LoadAvg()
	})),
)

var procRsc = hateoas.NewResource(
	hateoas.Path("/processes"),
	hateoas.GET(mohttp.DataHandlerFunc(func(c context.Context) (interface{}, error) {
		return process.Pids()
	})),
)
Beispiel #8
0
// currentState returns the current state of system conforming
// to definitions
func currentState(d Def) State {
	s, err := host.HostInfo()
	if err != nil {
		log.Printf("failed to load host information. (%v)", err.Error())
		return State{}
	}

	state := State{
		Host:      s.Hostname,
		Uptime:    s.Uptime,
		Ports:     []Port{},
		Sockets:   []Socket{},
		Processes: []Process{},
	}

	pids, err := process.Pids()
	if err != nil {
		log.Printf("failed to query running processes. (%v)", err.Error())
		return State{}
	}

	for _, i := range pids {
		p, err := process.NewProcess(i)
		if err != nil {
			log.Printf("failed to get handle of process with ID: %v. (%v)", i, err.Error())
			continue
		}

		n, _ := p.Name()
		cmd, err := p.Cmdline()
		if err != nil {
			log.Printf("failed to query the command which started the process with ID: %v. (%v)", i, err.Error())
			continue
		}

		cnt := false
		for k := range d.Processes {
			if strings.Contains(n, k) {
				cnt = true
				break
			}
		}

		conn, err := p.Connections()
		if err != nil {
			log.Printf("failed to query connections made by process with ID: %v. (%v)", i, err.Error())
			continue
		}

		pr := Process{
			Name:        n,
			Command:     cmd,
			Connections: []Conn{},
		}

		for _, c := range conn {
			ncn := Conn{Local: c.Laddr, Remote: c.Raddr}
			pr.Connections = append(pr.Connections, ncn)

			if _, match := d.Ports[ncn.Local.Port]; match {
				state.Ports = append(state.Ports, Port(ncn.Remote.Port))
			}

			if cnt {
				state.Processes = append(state.Processes, pr)
			}
		}
	}

	for k := range d.Sockets {
		if _, err := os.Stat(k); err != nil {
			state.Sockets = append(state.Sockets, Socket(k))
		}
	}

	return state
}