Operating System - Linux
1820529 Members
2114 Online
109626 Solutions
New Discussion юеВ

Re: How do i get current cpu and memory utilization in linux using the c programming

 
nataraju K R_1
New Member

How do i get current cpu and memory utilization in linux using the c programming

I am writting the snmp agent for one device fo r that i need to send the traps when cpu and memory utilization execded the set thresholds for this i want know how do i get the current cpu and memory utilization using the c programs. If you have any progrmas please forward to me.

Thanks in advance


Thanks and Regards
Nataraju K R
3 REPLIES 3
Ross Minkov
Esteemed Contributor

Re: How do i get current cpu and memory utilization in linux using the c programming

You can call external commands to get utilization info (say vmstat) from your program, get the output in and parse it.

If you need an snmp agent to monitor cpu and mem util just use net-snmp. Once installed you can set your thresholds in snmpd.conf.

-Ross
Bill Thorsteinson
Honored Contributor

Re: How do i get current cpu and memory utilization in linux using the c programming

Check the code for uptime, and free.
Both are minimal programs accessing the
appropriate data from the kernel.
Hein van den Heuvel
Honored Contributor

Re: How do i get current cpu and memory utilization in linux using the c programming


I am sure there are dozens of agents written that already do what you are going to invent, and a lot more, and a lot more secure.
Maybe look at places like: http://net-snmp.sourceforge.net/

Still, of you want to only partially reinvent this wheel, check out sources for sar/top/vmstat.
For example:

http://cvs.sourceforge.net/viewcvs.py/*checkout*/procps/procps/vmstat.c?rev=1.30


fwiw,
Hein.