English is not my mother tongue, this page may contain incorrect words or sentences.

urriellu.net => Articles => Software => Scripts for cacti

Cacti is a rrdtool frontend that makes easy the creation and administration of graphs, mainly used for showing info about a computer or network.

CPU temperature

This script retrieves local CPU temperature. You need to enable ACPI and know the path to the file that shows the CPU temp. By default it is set to /proc/acpi/thermal_zone/THRM/temperature. It just runs on Linux (or Unix-like).

cputemp.py, 8 lines      [download]

  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. TemperatureString=open('/proc/acpi/thermal_zone/THRM/temperature', 'ro')
  5. CpuTempArray=TemperatureString.readline().split("             ")
  6. CpuTempArray2=CpuTempArray[1].split(" ") #I know, too much code, but is easy to understand the code in this way
  7. CpuTemp=CpuTempArray2[0]
  8. print CpuTemp
  9.  

Templates required by cacti for graphs generation:

cacti_data_template_linux_cpu_temperature.xml, 81 lines      [download]

  1. <cacti> 
  2.         <hash_0100129ce4ea160a6bc98071432804c815133b>
  3.                 <name>Linux - CPU Temperature</name>
  4.                 <ds>
  5.                         <t_name></t_name>
  6.                         <name>|host_description| - CPU Temperature</name>
  7.                         <data_input_id>hash_030012ed73b72e78600194d76aeb45acba6c91</data_input_id>
  8.                         <t_rra_id></t_rra_id>
  9.                         <t_rrd_step></t_rrd_step>
  10.                         <rrd_step>300</rrd_step>
  11.                         <t_active></t_active>
  12.                         <active>on</active>
  13.                         <rra_items>hash_150012c21df5178e5c955013591239eb0afd46|hash_1500120d9c0af8b8acdc7807943937b3208e29|hash_1500126fc2d038fb42950138b0ce3e9874cc60|hash_150012e36f3adb9f152adfa5dc50fd2b23337e</rra_items>
  14.                 </ds>
  15.                 <items>
  16.  
  17. [...]

cacti_graph_template_linux_cpu_temperature.xml, 192 lines      [download]

  1. <cacti> 
  2.         <hash_000012a27d0a0c50135897c3512003842ee4b7>
  3.                 <name>Linux - CPU Temperature</name>
  4.                 <graph>
  5.                         <t_title></t_title>
  6.                         <title>|host_description| - CPU Temperature</title>
  7.                         <t_image_format_id></t_image_format_id>
  8.                         <image_format_id>1</image_format_id>
  9.                         <t_height></t_height>
  10.                         <height>120</height>
  11.                         <t_width></t_width>
  12.                         <width>500</width>
  13.                         <t_auto_scale></t_auto_scale>
  14.                         <auto_scale>on</auto_scale>
  15.                         <t_auto_scale_opts></t_auto_scale_opts>
  16.  
  17. [...]

uptime

This script gets the uptime of the local system. Only for linux:

uptime.py, 7 lines      [download]

  1. #!/usr/bin/python
  2. # -*- coding: UTF-8 -*-
  3.  
  4. UptimeString=open('/proc/uptime', 'ro')
  5. UptimeStrings=UptimeString.readline().split(" ")
  6. UptimeTotal=eval(UptimeStrings[0])
  7. UptimeIdle=eval(UptimeStrings[1])
  8. print "up_total:"+str(UptimeTotal/(60.*60.*24.))+" up_idle:"+str(UptimeIdle/(60.*60.*24.))

And cacti templates:

cacti_data_template_linux_uptime.xml, 104 lines      [download]

  1. <cacti> 
  2.         <hash_0100123d1c7767910ab74d09e7dcda0c21cc6a>
  3.                 <name>Linux - Uptime</name>
  4.                 <ds>
  5.                         <t_name></t_name>
  6.                         <name>|host_description| - Uptime</name>
  7.                         <data_input_id>hash_030012cf27b91d2d60b82d92209f123ccc43cd</data_input_id>
  8.                         <t_rra_id></t_rra_id>
  9.                         <t_rrd_step></t_rrd_step>
  10.                         <rrd_step>300</rrd_step>
  11.                         <t_active></t_active>
  12.                         <active>on</active>
  13.                         <rra_items>hash_150012c21df5178e5c955013591239eb0afd46|hash_1500120d9c0af8b8acdc7807943937b3208e29|hash_1500126fc2d038fb42950138b0ce3e9874cc60|hash_150012e36f3adb9f152adfa5dc50fd2b23337e</rra_items>
  14.                 </ds>
  15.                 <items>
  16.  
  17. [...]

cacti_graph_template_linux_uptime.xml, 233 lines      [download]

  1. <cacti> 
  2.         <hash_00001268bbf93247900d33874f4ad3b553adb0>
  3.                 <name>Linux - Uptime</name>
  4.                 <graph>
  5.                         <t_title></t_title>
  6.                         <title>|host_description| - Uptime</title>
  7.                         <t_image_format_id></t_image_format_id>
  8.                         <image_format_id>1</image_format_id>
  9.                         <t_height></t_height>
  10.                         <height>120</height>
  11.                         <t_width></t_width>
  12.                         <width>500</width>
  13.                         <t_auto_scale></t_auto_scale>
  14.                         <auto_scale>on</auto_scale>
  15.                         <t_auto_scale_opts></t_auto_scale_opts>
  16.  
  17. [...]