collector

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2024 License: MIT Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FilePath    = "/proc/sys/fs/file-max"
	VersionPath = "/proc/version"
	StatPath    = "/proc/stat"
	PtsPath     = "/dev/pts"
)
View Source
const (
	DevPath      = "/proc/net/dev"
	CollsIndex   = 13
	CarrierIndex = 14
	SumLength    = 16

	NetCoutnerMaxSize = math.MaxUint64
)
View Source
const (
	TCP_ESTABLISHED = iota + 1
	TCP_SYN_SENT
	TCP_SYN_RECV
	TCP_FIN_WAIT1
	TCP_FIN_WAIT2
	TCP_TIME_WAIT
	TCP_CLOSE
	TCP_CLOSE_WAIT
	TCP_LAST_ACK
	TCP_LISTEN  // 0x0A
	TCP_CLOSING // now a valid state
	TCP_MAX_STATES
)

tcp status reference: source/include/net/tcp_states.h

 enum {
	TCP_ESTABLISHED = 1,
	TCP_SYN_SENT,
	TCP_SYN_RECV,
	TCP_FIN_WAIT1,
	TCP_FIN_WAIT2,
	TCP_TIME_WAIT,
	TCP_CLOSE,
	TCP_CLOSE_WAIT,
	TCP_LAST_ACK,
	TCP_LISTEN,
	TCP_CLOSING,	* Now a valid state *
	TCP_NEW_SYN_RECV,   // kernel > 4.1

	TCP_MAX_STATES	* Leave at the end! *
};
View Source
const (
	SectorSize = 512
)
View Source
const TCPDIAG_GETSOCK = 18
Socket identity

struct inet_diag_sockid {
    __be16  idiag_sport;
    __be16  idiag_dport;
    __be32  idiag_src[4];
    __be32  idiag_dst[4];
    __u32   idiag_if;
    __u32   idiag_cookie[2];

#define INET_DIAG_NOCOOKIE (~0U) };

/* Request structure

struct inet_diag_req {
    __u8    idiag_family;       /* Family of addresses.
    __u8    idiag_src_len;
    __u8    idiag_dst_len;
    __u8    idiag_ext;      /* Query extended information

    struct inet_diag_sockid id;

    __u32   idiag_states;
    __u32   idiag_dbs;
};

struct inet_diag_msg {
    __u8    idiag_family;
    __u8    idiag_state;
    __u8    idiag_timer;
    __u8    idiag_retrans;

    struct inet_diag_sockid id;

    __u32   idiag_expires;
    __u32   idiag_rqueue;
    __u32   idiag_wqueue;
    __u32   idiag_uid;
    __u32   idiag_inode;
};

Variables

This section is empty.

Functions

func CheckBlackWhiteList

func CheckBlackWhiteList(name string, whiteList []*regexp.Regexp, blackList []*regexp.Regexp) bool

根据黑白名单检查是否对应的数据应该上报,返回true则应上报,false则不应上报

func CheckInSimpleList

func CheckInSimpleList(name string, list []*regexp.Regexp) bool

func CheckInStringList

func CheckInStringList(name string, list []*regexp.Regexp) bool

func CounterDiff

func CounterDiff(now, before uint64) uint64

func FilterDiskIoStats

func FilterDiskIoStats(diskStats map[string]DiskStats, config configs.DiskConfig) map[string]DiskStats

FilterDiskIoStats 过滤磁盘io信息

func FilterNetIOStats

func FilterNetIOStats(ioCounterStat []net.IOCountersStat, config configs.NetConfig) []net.IOCountersStat

func FilterNetInterfaceStats

func FilterNetInterfaceStats(interfaceStats []net.InterfaceStat, config configs.NetConfig) []net.InterfaceStat

func FilterPartitions

func FilterPartitions(partitionStats []disk.PartitionStat, config configs.DiskConfig) []disk.PartitionStat

FilterPartitions :

func GetCpuCores

func GetCpuCores() (int32, error)

GetCpuCores cpu count * cores count of per cpu

func GetDateTime

func GetDateTime(tolerate int64) (string, string, int)

func GetDiskSpeed

func GetDiskSpeed(last, current map[string]DiskStats)

func GetLoginUsers

func GetLoginUsers() (int, error)

GetLoginUsers: 获取当前登录的用户数量,通过遍历/dev/pts下的终端数量,判断用户个数 返回内容是map,key为用户名,value为该用户登录的数量,以便统计

func GetMaxFiles

func GetMaxFiles() (int, error)

cat /proc/sys/fs/file-max

func GetNetInfoFromDev

func GetNetInfoFromDev() (map[string]NetInfo, error)

cat /proc/net/dev

func GetProcEnv

func GetProcEnv() (runningProc, blockedProc, proc, ctxt int, lasterr error)

func GetSwapInfo

func GetSwapInfo() (in, out float64, lasterr error)

func GetSwapinfoLogic

func GetSwapinfoLogic(sinfo *swapinfo) (*swapinfo, error)

func GetUname

func GetUname() (string, error)

cat /proc/version

func IOCounters

func IOCounters(names ...string) (map[string]DiskStats, error)

func PhysicalMemoryInfo

func PhysicalMemoryInfo(specialSource bool) (info *mem.VirtualMemoryStat, err error)

func ProtoCounters

func ProtoCounters(protocols []string) ([]net.ProtoCountersStat, error)

Types

type BKInfoStat

type BKInfoStat struct {
	*host.InfoStat
	SystemType string `json:"systemtype"`
}

add systemtype info

type BaseSocketInfo

type BaseSocketInfo struct {
	Stat    uint8
	SrcPort uint16
	DstPort uint16
	SrcIp   uint32
	DstIp   uint32
}

type CpuReport

type CpuReport struct {
	Cpuinfo    []cpu.InfoStat  `json:"cpuinfo"`
	Usage      []float64       `json:"per_usage"`
	TotalUsage float64         `json:"total_usage"`
	Stat       []cpu.TimesStat `json:"per_stat"`
	TotalStat  cpu.TimesStat   `json:"total_stat"`
}

func GetCPUInfo

func GetCPUInfo(config configs.CpuConfig) (*CpuReport, error)

type DiskReport

type DiskReport struct {
	DiskStats  map[string]DiskStats `json:"diskstat"`
	Partitions []disk.PartitionStat `json:"partition"`
	Usage      []disk.UsageStat     `json:"usage"`
}

func GetDiskInfo

func GetDiskInfo(config configs.DiskConfig) (*DiskReport, error)

func (*DiskReport) AssignDiskStats

func (report *DiskReport) AssignDiskStats(stats map[string]DiskStats)

type DiskStats

type DiskStats struct {
	ReadCount        uint64  `json:"readCount"`
	MergedReadCount  uint64  `json:"mergedReadCount"`
	WriteCount       uint64  `json:"writeCount"`
	MergedWriteCount uint64  `json:"mergedWriteCount"`
	ReadBytes        uint64  `json:"readBytes"`
	WriteBytes       uint64  `json:"writeBytes"`
	ReadTime         uint64  `json:"readTime"`
	WriteTime        uint64  `json:"writeTime"`
	IopsInProgress   uint64  `json:"iopsInProgress"`
	IoTime           uint64  `json:"ioTime"`
	WeightedIO       uint64  `json:"weightedIO"`
	Name             string  `json:"name"`
	SerialNumber     string  `json:"serialNumber"`
	Label            string  `json:"label"`
	MajorNum         uint64  `json:"major"`
	MinorNum         uint64  `json:"minor"`
	ReadSectors      uint64  `json:"readSectors"`
	WriteSectors     uint64  `json:"writeSectors"`
	SpeedIORead      float64 `json:"speedIORead"`
	SpeedByteRead    float64 `json:"speedByteRead"`
	SpeedIOWrite     float64 `json:"speedIOWrite"`
	SpeedByteWrite   float64 `json:"speedByteWrite"`
	Util             float64 `json:"util"`
	AvgrqSz          float64 `json:"avgrq_sz"`
	AvgquSz          float64 `json:"avgqu_sz"`
	Await            float64 `json:"await"`
	Svctm            float64 `json:"svctm"`
}

func ToDiskStats

func ToDiskStats(stats disk.IOCountersStat) DiskStats

type ElementInfo

type ElementInfo struct {
	Element []SocketInfo
}

store all tcp\udp socket info

type EnvReport

type EnvReport struct {
	Contab      []toolkit.Crontab `json:"crontab"`
	Host        string            `json:"host"`
	Route       string            `json:"route"`
	MaxFiles    int               `json:"maxfiles"`
	Uname       string            `json:"uname"`
	LoginUser   int               `json:"login_user"`
	RunningProc int               `json:"proc_running_current"`
	BlockedProc int               `json:"procs_blocked_current"`
	Totalproc   int               `json:"procs_processes_total"`
	Ctxt        int               `json:"procs_ctxt_total"`
}

func GetEnvInfo

func GetEnvInfo(config configs.BasereportConfig) (*EnvReport, error)

type FileSocketItem

type FileSocketItem struct {
	Laddr  string
	Raddr  string
	Status string
	Inode  string
	Pid    int32
	Fd     uint32
}

type InetDiagMsg

type InetDiagMsg struct {
	Family  uint8 // 0,1
	State   uint8 // 1,2
	Timer   uint8 // 2,3
	Retrans uint8 // 3,4

	Sport  uint16 // 4,6
	Dport  uint16 // 6,8
	Src    [4]uint32
	Dst    [4]uint32
	If     uint32
	Cookie [2]uint32

	Expires uint32
	Rqueue  uint32
	Wqueue  uint32
	Uid     uint32
	Inode   uint32
}

Inet_diag_msg represents recv msg data

type InetDiagReq

type InetDiagReq struct {
	Family uint8
	SrcLen uint8
	DstLen uint8
	Ext    uint8

	Sport  uint16
	Dport  uint16
	Src    [4]uint32
	Dst    [4]uint32
	If     uint32
	Cookie [2]uint32

	States uint32
	Dbs    uint32
}

Inet_diag_req represents request msg data

func (InetDiagReq) MarshalBinary

func (m InetDiagReq) MarshalBinary() []byte

MarshalBinary marshals a Message into a byte slice.

type JobMgr

type JobMgr struct {
	// contains filtered or unexported fields
}

func (*JobMgr) MarkFinished

func (jm *JobMgr) MarkFinished()

func (*JobMgr) MarkWork

func (jm *JobMgr) MarkWork()

func (*JobMgr) Running

func (jm *JobMgr) Running() bool

type LoadReport

type LoadReport struct {
	LoadAvg    *load.AvgStat `json:"load_avg"`
	PerCpuLoad float64       `json:"per_cpu_load"`
}

func GetLoadInfo

func GetLoadInfo() (*LoadReport, error)

GetLoadInfo

type MemReport

type MemReport struct {
	Swapin   float64                `json:"swap_in"`
	Swapout  float64                `json:"swap_out"`
	Info     *mem.VirtualMemoryStat `json:"meminfo"`
	SwapInfo *mem.SwapMemoryStat    `json:"vmstat"`
}

func GetMemInfo

func GetMemInfo(config configs.MemConfig) (*MemReport, error)

type NetInfo

type NetInfo struct {
	Errors     uint64 `json:"errors"`
	Dropped    uint64 `json:"dropped"`
	Fifo       uint64 `json:"overruns"`
	Carrier    uint64 `json:"carrier"`
	Collisions uint64 `json:"collisions"`
}

type NetReport

type NetReport struct {
	Interface []net.InterfaceStat         `json:"interface"`
	Stat      []Stat                      `json:"dev"`
	Netstat   SocketStatusCount           `json:"netstat"`
	Protocol  map[string]map[string]int64 `json:"protocolstat"`
}

func GetNetInfo

func GetNetInfo(config configs.NetConfig) (*NetReport, error)

type NoneSocketFilter

type NoneSocketFilter struct{}

NoneSocketFilter will return all sockets

func (NoneSocketFilter) Filter

func (f NoneSocketFilter) Filter(socket SocketInfo) bool

type ReportData

type ReportData struct {
	bkcommon.DateTime
	Cpu    *CpuReport    `json:"cpu"`
	Env    *EnvReport    `json:"env"`
	Disk   *DiskReport   `json:"disk"`
	Load   *LoadReport   `json:"load"`
	Mem    *MemReport    `json:"mem"`
	Net    *NetReport    `json:"net"`
	System *SystemReport `json:"system"`
}

func Collect

func Collect(config configs.BasereportConfig, firstRun bool) (ReportData, error)

type SocketFilter

type SocketFilter interface {
	// return true when you want the msg
	Filter(socket SocketInfo) bool
}

type SocketInfo

type SocketInfo struct {
	BaseSocketInfo
	Inode uint64
	Type  uint32 // syscall.SOCK_STREAM or syscall.SOCK_DGR
}

type SocketStatusCount

type SocketStatusCount struct {
	Established uint `json:"established"`
	SyncSent    uint `json:"syncSent"`
	SynRecv     uint `json:"synRecv"`
	FinWait1    uint `json:"finWait1"`
	FinWait2    uint `json:"finWait2"`
	TimeWait    uint `json:"timeWait"`
	Close       uint `json:"close"`
	CloseWait   uint `json:"closeWait"`
	LastAck     uint `json:"lastAck"`
	Listen      uint `json:"listen"`
	Closing     uint `json:"closing"`
}

func GetTcp4SocketStatusCount

func GetTcp4SocketStatusCount() (SocketStatusCount, error)

GetTcp4SocketStatusCount get sockets status

func GetTcp4SocketStatusCountByNetlink() (SocketStatusCount, error)

GetTcp4SocketStatusCountByNetlink get sockets status by netlink

type Stat

type Stat struct {
	net.IOCountersStat
	NetInfo

	SpeedSent        uint64 `json:"speedSent"`        // speed of sent, bytes/second
	SpeedRecv        uint64 `json:"speedRecv"`        // speed of received, bytes/second
	SpeedPacketsSent uint64 `json:"speedPacketsSent"` // speed of packets sent, nr/second
	SpeedPacketsRecv uint64 `json:"speedPacketsRecv"` // speed of packets received, nr/second
}

type SystemReport

type SystemReport struct {
	Info BKInfoStat `json:"info"`
}

func GetSystemInfo

func GetSystemInfo() (*SystemReport, error)

type TcpSocketListenFilter

type TcpSocketListenFilter struct{}

TcpSocketListenFilter only return listen status tcp sockets

func (TcpSocketListenFilter) Filter

func (f TcpSocketListenFilter) Filter(socket SocketInfo) bool

type TcpSocketListenPortFilter

type TcpSocketListenPortFilter struct {
	ListenPorts map[uint16]bool
}

TcpSocketListenPortFilter filters

func (TcpSocketListenPortFilter) Filter

func (f TcpSocketListenPortFilter) Filter(socket SocketInfo) bool

type UdpSocketListenPortFilter

type UdpSocketListenPortFilter struct {
	ListenPorts map[uint16]bool
}

UdpSocketListenPortFilter filters

func (UdpSocketListenPortFilter) Filter

func (f UdpSocketListenPortFilter) Filter(socket SocketInfo) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL