Question

SNMP message to fetch the %CPU and %Memory

Asked by: muralimohanb


Hi,

We have a requirement to fetch the average %CPU and memory used on a UNIX/Linux server for every 5 minutes.

I learnt that through SNMP we can get these values.  For ex:
snmpget -O v -c private localhost ssCpuRawSystem.0

I am trying to write a program in Java which should be able to send the SNMP message to and receive the response SNMP message from the server. I learnt that SNMP uses the User Datagram Protocol. But I am unaware how this DatagramSockets can be used and how to format the data to be sent to SNMP server? It seems we have to encode/decode the data.

Please let me know if anybody have implemented such a function?? Please respond as soon as possible!
Thanks and Regards
Murali

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2004-07-22 at 21:31:30ID21068722
Tags

snmp

Topic

Java Programming Language

Participating Experts
2
Points
500
Comments
29

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. LINUX/UNIX %CPU and Memory via SNMP
    Hi, We have a requirement to fetch the average %CPU and memory used on a UNIX/Linux server for every 5 minutes. I have tried the sar command succesfully, but some times it is faling for some reasons. I learnt that through SNMP we can get these values. Can anybody please l...
  2. SNMP
    How do i install SNMP on WINXP PRO XP2

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: objectsPosted on 2004-07-22 at 21:37:02ID: 11618791

 

by: Giant2Posted on 2004-07-23 at 00:21:40ID: 11619310

>I am trying to write a program in Java which should be able to send the SNMP message to and receive the response SNMP message from the server. I learnt that SNMP uses the User Datagram Protocol. But I am unaware how this DatagramSockets can be used and how to format the data to be sent to SNMP server?

Some times ago I used JoeSnmp to do this.

>It seems we have to encode/decode the data.

Sure!

Giant.

 

by: Giant2Posted on 2004-07-23 at 00:24:41ID: 11619328

 

by: Giant2Posted on 2004-07-23 at 00:25:38ID: 11619338

 

by: Giant2Posted on 2004-07-26 at 23:41:21ID: 11643808

Do you solved?

 

by: muralimohanbPosted on 2004-07-27 at 00:01:26ID: 11643892

Hi Giant2

Not yet. I am looking into joeSnmp and other packages. I have to evoluate which is best for our project. My be by this weekend, I can come to some conclusion.

Thanks for your concern.
Regards
Murali

 

by: Giant2Posted on 2004-07-27 at 00:20:57ID: 11643978

Ok.
Post what is your final decision.
Thanks.

 

by: muralimohanbPosted on 2004-07-27 at 00:33:33ID: 11644032

Hi Giant2,

Please clarify me one thing. I am trying to understand what the joeSNMP can do.

With joeSnmp, can I get the system statistics of a remote server? I mean I will run a program on a manager machine (Could be a windows system with the joeSnmp jar package) and get the system statistics of a remote server?

Regards
Murali

 

by: Giant2Posted on 2004-07-27 at 00:42:13ID: 11644078

SNMP is divided in 2 sides: client and server.
The server could not be directly done with JoeSNMP (I did one working over the JoeSNMP api).
The client could be completely done with JoeSNMP because it implement the ASN.1 ans SNMP level 2.

If you want system statistic on a server, on this server must be present an SNMPAgent (Linux and Windows gave a standard SNMPv2 agent, so if you want other data you must run over the server another SNMPAgent).

With JoeSNMP you can send the request to the Agent (on the server) and take the answer in about easyway.

 

by: muralimohanbPosted on 2004-07-27 at 18:35:51ID: 11652580

Hi Giant2,

How far is JoeSNMP reliable?

Regards
Murali

 

by: Giant2Posted on 2004-07-27 at 23:30:55ID: 11653865

I believe 99%. I tested for building the Agent and it goes well.
I didn't notice any defeating. It was not full because you must implement something, but it's good.
Building a ServerAgent the work must be really little big, but for the client (I understand you want to do this), it's a good API.

 

by: muralimohanbPosted on 2004-07-27 at 23:38:42ID: 11653902

Hi Giant2,

Yeah, I want just the client part.

I was going thrpugh the test java program snmpwalk.class to get the ssCpuRawUser values etc,. This program appears to be taking only one OID at a time. For ex:

java org.opennms.test.snmpwalk -v 2 -c public -r 2 -t 10 -p 161 172.17.12.76 1.3.6.1.4.1.2021.11.50

1.3.6.1.4.1.2021.11.50 is for ssCpuRawUser.

I have two doubts here:

1. Can I fetch the values of ssCpuRawUser, ssCpuRawSystem, ssCpuRawIdle etc simultaneouly within one command? WIth the snmpget comman we can get do this by the following command:

snmpget -O v -c private localhost ssCpuRawSystem.0 ssCpuRawIdle.0 ssCpuRawUser.0

Can the same be achived by this joeSnmp?

2. I need to get the average %CPU usage for every 5 mins period. How can I achieve this?

Please help me in this regard. Its really urgent and I am worrying a lot as this is already delayed for me.

Thanks and Regards
Murali

 

by: Giant2Posted on 2004-07-27 at 23:52:04ID: 11653972

>1. Can I fetch the values of ssCpuRawUser, ssCpuRawSystem, ssCpuRawIdle etc simultaneouly within one command? >WIth the snmpget comman we can get do this by the following command:

Maybe yes. It's possible to take some OID simultaneously. I don't remember if snmpget of JoeSNMP implement. (I checkit in the meantime).
But you can rewrite your own what snmpget do.

>2. I need to get the average %CPU usage for every 5 mins period. How can I achieve this?

I did something like this. I used a Timer (java.util.Timer) scheduled every 10 seconds. in the run method I made the getSNMP to the server.
SNMP is done only to get and set values, not to schedule task. So you must implement your own this feature.

 

by: muralimohanbPosted on 2004-07-27 at 23:57:53ID: 11654004

Hi  Giant2

I can schdule the task programatically ( implementing a thread ). The problem is getting the required values through snmpget.

>Maybe yes. It's possible to take some OID simultaneously. I don't remember if snmpget of JoeSNMP implement. (I checkit in the meantime). But you can rewrite your own what snmpget do.

Do you mean, that I can write some thing like snmpget.java on my own similar to snmpwalk.java?

Please give me some sample source code u have implemented for both of my earlier requests.

Thanks and Regards
Murali

 

by: Giant2Posted on 2004-07-28 at 00:06:41ID: 11654041

for multiple OID request see the "snmpwalkmv" taking with JoeSNMP sourcecode. In it you can specify many OID.

 

by: Giant2Posted on 2004-07-28 at 00:12:02ID: 11654067


...your code...

 
private Timer poll=new Timer();

public void polling(){
    logger.info("Starting polling");
    try{
      poll.cancel();
      poll.scheduleAtFixedRate(getNewPollingTimTask(), 0, 5000);//wait 5 secondi before starting polling
      }catch (java.lang.IllegalStateException ise){//the timer cannot be scheduled
        poll=new Timer();
        poll.scheduleAtFixedRate(getNewPollingTimTask(), 0, 5000);//wait 5 secondi before starting polling
        }
    }

  //create a TimerTask
  private TimerTask getNewPollingTimTask(){
    TimerTask tt=new TimerTask() {
      boolean firstPol=true;
      public void run() {
          if (!firstPol){
            logger.info("Starting poll");
            //CALL HERE YOUR METHOD TO GET THE VALUE USING SNMP
            long delay=50000;
            try{
              poll.cancel();
              poll.schedule(getNewPollingTimTask(),0,delay);//re-schedule after delay milliseconds
              }
            catch (java.lang.IllegalStateException ise){
              poll=new Timer();
              poll.schedule(getNewPollingTimTask(),0,delay);//re-schedule after delay milliseconds
              }
            }
          firstPol=false;
        }
      };
    return tt;
    }


Hope this help you.
Bye, Giant.

 

by: muralimohanbPosted on 2004-07-29 at 18:32:01ID: 11673092

Hi Giant2 ,

Thanks for the code sample. I am sure u gave this is for scheduler. I am implementing a thread for scheduling.

I have modified the snmpwalk.java and wrote a snmpget.java class which can acept many OIDs and get the values. I could successfully fetch the values.

Now my problem is how to calculate the %Cpu of the User application for a period of 1 minute?

For ex: I could fetch the ssCpuRawIdle, ssCpuRawUser and ssCpuRawSystem at 10:00:00 Hrs. These values are say  6413622,  35307  and 6591 respectively. 
After 1 minute, i.e. at 10:01:00 hrs, the values are 6422453,  35407 and 6605 respectively. 

With these values, I could not make a formula which can give me the % average CPU usage by User applications? Can you please give me some idea?

Thanks and Regards
Murali

 

by: Giant2Posted on 2004-07-29 at 23:09:31ID: 11674079

I did the same things asking to a Linux machine. At my project I let to calculate the average to the Linux server. (using the right MIB)
I believe if you want t o implement on the client side at each instant you cannot have the right average, but the average calculated from the first snmpget you did.
You can calculate the average following 2 ways:
1) create 2 variable: one for saving the total and one saving the number of SNMPget you did.
For each snmget you did: add to total the value you read; add 1 to numOfSNMPget.
The average will be total/numOfSNMPget

2) (but it's not really a real average) create 1 variable named "average".
For each snmget you did: add to average the value you read; make this instruction: average=average/2.
In the average variable there'll be the average calculated (but, I repeat, it's not a real average)

If you are interested to the average of use of a machine, I repeat the previous suggested.
Find some MIB(and Agent obviously) do this for you. So the only thing you must do to know the average is only a getSNMP.

Hope this help you.
Bye, Giant.

 

by: Giant2Posted on 2004-08-01 at 22:57:05ID: 11691100

Do you solved?

 

by: muralimohanbPosted on 2004-08-02 at 21:12:45ID: 11700579

Hi Giant2,

I am still working on this. I could make the snmpget class and simultaneously getting the values. I found in http://www.net-smp.org/FAQ.html on how to caclulate the % CPU Utilization. I am right now working on this. I hope this will be over soon.

Regards
Murali

 

by: Giant2Posted on 2004-08-04 at 07:20:47ID: 11715838

sorry, but the address you posted is not reachable.

 

by: Giant2Posted on 2004-08-04 at 07:35:48ID: 11715982

Whow, now I see the link.

 

by: muralimohanbPosted on 2004-08-04 at 17:20:42ID: 11722108

Hi Giant2,

BTW, I could succesfully get the values of CPU usage. But the memory available is giving a problem. On t he SNMP agent side ( i.e. server side ) when I execute the following command,
snmpget -c private localhost memAvailReal.0
it is always resulting in the following value:

UCD-SNMP-MIB::memAvailReal.0 = INTEGER: -1

Any idea how to fetch the memAvailReal?

Regards
Murali

 

by: Giant2Posted on 2004-08-05 at 00:09:15ID: 11723748

UCD-SNMP-MIB, so you follow my suggestion:
>Find some MIB(and Agent obviously) do this for you.
:)


I see you use
memAvailReal.0

try to use the oid representation of memAvailReal. Sometimes agent doesn't support the name casting.

Do you have the same problem with other oid? If yes, probably the agent (server side) doesn't go well.

 

by: muralimohanbPosted on 2004-08-05 at 01:31:37ID: 11724093

I tried that also like

snmpget -c private localhost 1.3.6.1.4.1.2021.4.6.0

There is no problem with other OIDs.  My server is Redhat Linux. Is there any problem with Redhat Linux?

Murali

 

by: Giant2Posted on 2004-08-05 at 01:33:51ID: 11724104

As I told before, probably the agent (server side) doesn't go well.
Check the Agent state and features enabled.

 

by: muralimohanbPosted on 2004-08-05 at 17:21:30ID: 11731962

Please tell me how can I check whether the feature for memory MIBs enabled or not?

Regards
Murali

 

by: Giant2Posted on 2004-08-05 at 23:09:09ID: 11733140

Sure.
The MIB you use is connected to an Agent.
The Agent has a producer. So you can see to what producer says about it.
Normally (in Linux) there are separated modules which take the data from the system environment (access to localhost files).
But to know what file and where, you must search to the producer documentation.
Each Agent could have a different way to access these data.

 

by: Giant2Posted on 2004-08-22 at 22:58:15ID: 11867525

Thanks.
Happy to help you.
:-)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...