cpu_utilization
The following two tabs change content below.
Hi, I have written and developed this site to share my experience and ideas with other colleagues. I also started to prepare interview questions and answers for job seekers. I hope it will help you a lot.

That’s true we can monitor CPU in zabbix using SNMP in all environment but we can monitor only core CPU not overall CPU utilization. Just now I have finished to monitor CPU overall utlization in my current project. So I thought to share the same with you guys might be this will help to someone.

In this post we will learn how to achive that. To discover simple core CPU i am using SNMP agent. Lets start.

1.In the starting I hope you all are aware about how to onboard a device in zabbix and how to create a dicovery and prototype items.

2. As we know there is no OID to get CPU overall utilization so for this creating item with type “External check” and in that we will call linux shell file. and in linux shell file we will run the CURL command to call Java API in which we will send host name and will get the overall CPU utilization.

3. Lets create shell file on zabbix server under “/usr/lib/zabbix/externalscripts” location.

Note: If device we are monitoring through zabbix proxy then this external script file should be copy on proxy server rather then main zabbix server. And ExternalScripts file location we can change from zabbix_server.conf file or zabbix_proxy.conf.

external_script

4. We have file “total_cpu_util.sh” and inside this file we have below curl command

curl -s -d '{"output":"extend","host":"'$1'"}' -H "Content-Type: application/json" -X POST http://<<Host/IP>>/<<PROJECT_NAME>>/restservice/getOverallUtilization/$2

In this .sh file I have passed two runtime parameter “$1” will accept HostName from zabbix item and “$2” will accept type of utilization its “CPU”. I kept second parameter to get and calculate other overall utilization like overall file system utilization etc.

Below is java API code :

/**
*
*author kpblogs
*
**/ 
@Controller
public class CPUUtilController {


 @Autowired
 private ZabbixAPIClient zabbixAPIClient;


 @RequestMapping(value = "/restservice/getOverallUtilization/{component}", method = RequestMethod.POST, produces = "application/json")
 @ResponseBody
 public String getOverallUtilization(@PathVariable String component, @RequestBody Object object) {
 return getOverallUtilization(object, component);
 }
 
 
 /**
 * 
 * @param hostName
 * @return
 */
 public String getOverallUtilization(Object object, String component) {
 
 int count = 0;
 float sum = 0;
 JsonObject object1 = new Gson().fromJson(object.toString(), JsonObject.class);
 Map<String, Object> map = new HashMap<String, Object>();
 map.put("host", object1.get("host"));
 
 
 Map<String, String[]> searchArray = new HashMap<String, String[]>();
 
 if (component.trim().equalsIgnoreCase("cpu")) {
 searchArray.put("key_", new String[]{"cpu.discovery[core"});
 }
 
 map.put("search", searchArray);
 map.put("searchByAny", "true");
 
 String auth = zabbixLoginTokn(zabbixAPIClient);

ItemGetResponse response = zabbixAPIClient.getItem(map, auth);
 //System.out.println(new Gson().toJson(response));
 if (response != null) {
 ItemResponse itemResponse[] = response.getResult();
 if (component.trim().equalsIgnoreCase("cpu")) {
 for (ItemResponse itemResponse2 : itemResponse) {
 if (itemResponse2.getKey_() != null) {
 if (itemResponse2.getKey_().trim().toLowerCase()
 .contains("cpu.discovery[core".trim().toLowerCase())) {
 count++;
 DecimalFormat format = new DecimalFormat();
 format.setMaximumFractionDigits(2);
 if (itemResponse2.getLastvalue().contains("%"))
 sum = sum + Float.parseFloat(itemResponse2.getLastvalue().split("%")[0]);
 else
 sum = sum + Float.parseFloat(itemResponse2.getLastvalue());
 }
 }

}
 } 
 }
 
 return String.format("%.02f", (sum / count));
 }
 
 }

I have you already setup java adapter to call zabbix API. And in the above code I have given the search in zabbix API and get all items who have key_ “cpu.discovery[core”. because duering the discovery rule/prototype item I have given this key only. If you use different key then please change in the above code also.

Once I got the all item then I calculated the number of items.

I know every code cpu have different item so means how many item we have so same number of core cpu be will have in that server.

Now to get overall CPU utilization I have applied below formula.

sum of all item’s value / total number of items. as given below:

return String.format("%.02f", (sum / count));

I hope you are clear now. lets create a external check item in zabbix under that server.

1. click on create item:

create_item

2. Now give the require details as given in below screenshot.

external_check_item

 

3. In the item i have given 300s (5 minutes). after this time we can again check the latest data under Monitoring. If all configuration is good you will see the overall cpu utilization as given.

overall_cpu_utilization

 

that’s all please share your feedback and also let me know if you need help on zabbix in any other part.

 

175 total views, 1 views today

Leave a Reply

Your email address will not be published. Required fields are marked *