#!/usr/bin/perl -w # # gpu_snmp.pl # # Description: # Script for accessing ATI GPU data statistics for temperature, load, # core clock and memory clock for any number of installed GPUs. Originally # based on a script found on http://saaboke.com/?p=264 and modified for bugfixes, # addition of memory clock monitoring and re-writen in Perl. # # Notes: # If you see "sudo: sorry, you must have a tty to run sudo" when executing # the script via snmpd, comment out the following line in your sudoers file: # "Default requiretty" # ###################### ## Revision History ## ###################### # # v0.01 10/11/2011 JinTu (jintuNO@SPAMpraecogito.com) # First working version. # use strict; use POSIX; ######################################################## ## Tweak the following variables for your environment # my $sudo = "sudo -u jintu"; # The sudo command to execute my $aticonfig = "/usr/bin/aticonfig"; # The full path to aticonfig my $display = "DISPLAY=:0"; # The display your X session and ATI GPUs are running under # ######################################################## ## ## Don't edit below here unless you know what you are doing ## ####################################################### # # To ensure we behave when we get an hangup signal $SIG{HUP} = \&safe_shutdown; alarm(30); # number of seconds to wait before forcing shutdown ########### # Check if we have been provided an appropriate argument, otherwise dump help and bail if (defined($ARGV[0])) { unless (($ARGV[0] eq "temp") || ($ARGV[0] eq "load") || ($ARGV[0] eq "clock") || ($ARGV[0] eq "memory") || ($ARGV[0] eq "vcore") || ($ARGV[0] eq "fan") || ($ARGV[0] eq "id") || ($ARGV[0] eq "address") || ($ARGV[0] eq "description")) { print_usage(); } } else { print_usage(); } ## list adapters. If we get an error, bail out my $adapter_list = `$sudo $display $aticonfig --lsa 2>&1`; # # Normal result looks like # * 0. 03:00.0 AMD Radeon HD 6900 Series # 1. 04:00.0 AMD Radeon HD 6900 Series # 2. 07:00.0 AMD Radeon HD 6900 Series # 3. 08:00.0 AMD Radeon HD 6900 Series # # * - Default adapter # unless ($adapter_list =~ m/^.*\d+\.\s+\d{2}:\d{2}\.\d\s+.*/) { # Bail out, we are getting an error die "Execution failed with:" . $adapter_list . "\nCheck that sudo and aticonfig are configured correctly.\n"; } # my ($id,$address,$description); my $num_adapters = 0; my @adapter; foreach my $line (split (/\n/,$adapter_list)) { if (($id,$address,$description) = $line =~ m/^.*(\d+)\.\s+(\d{2}:\d{2}\.\d)\s+(.*)/) { #print "Got \$id:$id,\$address:$address,\$description:$description\n"; $adapter[$num_adapters]{'id'} = $id; $adapter[$num_adapters]{'address'} = $address; $adapter[$num_adapters]{'description'} = $description; $num_adapters++; } #else { # print "Match failed for line: $line\n"; #} } # Now we are ready to do something with this info # if ($ARGV[0] eq "temp") { foreach my $adptr (@adapter) { my $cmd = "$sudo $display $aticonfig --odgt --adapter=" . $adptr->{'id'} . " | grep Temperature | awk '{print \$5}'"; my $result = `$cmd`; #print $adptr->{'id'} . ": $result"; print $result * 100 . "\n"; } } elsif ($ARGV[0] eq "load") { foreach my $adptr (@adapter) { my $cmd = "$sudo $display $aticonfig --odgc --adapter=" . $adptr->{'id'} . " | grep 'GPU load' | awk '{gsub(/%/,\"\",\$4); print \$4}'"; my $result = `$cmd`; #print $adptr->{'id'} . ": $result"; print $result; } } elsif ($ARGV[0] eq "clock") { foreach my $adptr (@adapter) { my $cmd = "$sudo $display $aticonfig --odgc --adapter=" . $adptr->{'id'} . " | grep 'Current Clocks' | awk '{print \$4}'"; my $result = `$cmd`; #print $adptr->{'id'} . ": $result"; print $result; } } elsif ($ARGV[0] eq "memory") { foreach my $adptr (@adapter) { my $cmd = "$sudo $display $aticonfig --odgc --adapter=" . $adptr->{'id'} . " | grep 'Current Clocks' | awk '{print \$5}'"; my $result = `$cmd`; #print $adptr->{'id'} . ": $result"; print $result; } } elsif ($ARGV[0] eq "vcore") { foreach my $adptr (@adapter) { my $cmd = "$sudo $display." . $adptr->{'id'} . " $aticonfig --pplib-cmd \"get activity\" | grep 'VDDC' | awk '{print \$2}'"; my $result = `$cmd`; #print $adptr->{'id'} . ": $result"; print $result; } } elsif ($ARGV[0] eq "fan") { foreach my $adptr (@adapter) { my $cmd = "$sudo $display." . $adptr->{'id'} . " $aticonfig --pplib-cmd \"get fanspeed 0\" | grep 'Fan Speed' | awk '{gsub(/%/,\"\",\$4); print \$4}'"; my $result = `$cmd`; #print $adptr->{'id'} . ": $result"; print $result; } } elsif ($ARGV[0] eq "id") { foreach my $adptr (@adapter) { print $adptr->{'id'} . "\n"; } } elsif ($ARGV[0] eq "address") { foreach my $adptr (@adapter) { print $adptr->{'address'} . "\n"; } } elsif ($ARGV[0] eq "description") { foreach my $adptr (@adapter) { print $adptr->{'description'} . "\n"; } } # utility subs sub print_usage { print "usage:\n\ngpu_snmp.pl \n\nWhere can be one of the following:\n"; print " temp - get GPU core temperature in C\n"; print " load - get GPU load in %\n"; print " clock - get current core clock in MHz\n"; print " memory - get current memory clock in MHz\n"; print " vcore - get current Vcore (GPU core voltage) in mV\n"; print " fan - get current fan speed in %\n"; print " id - get the adapter id\n"; print " address - get the adapter address\n"; print " description - get the adapter description\n"; exit; } sub safe_shutdown { # We had an alarm timeout, shut down exit(0); }