99_CT.pm 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # $Id: 99_CT.pm 5336 2014-03-27 01:08:49Z betateilchen $
  2. package main;
  3. use strict;
  4. use warnings;
  5. # define items and where/how they can be found
  6. my %handles = (
  7. # for each item define something like this:
  8. # ITEM => [
  9. # 'path containing the data we need',
  10. # 'regular expression matching the (bits) we need',
  11. # sub { to process the bits found by the regex },
  12. # ],
  13. # Clock frequency of CPU in Hz, e.g. 800000
  14. CPU0FREQ => [
  15. '/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq',
  16. '(\d+)',
  17. sub { shift },
  18. ],
  19. # Clock frequency of CPU in Hz, e.g. 800000
  20. CPU1FREQ => [
  21. '/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq',
  22. '(\d+)',
  23. sub { shift },
  24. ],
  25. # MAC address of wired ethernet connection
  26. LMAC => [
  27. '/sys/class/net/eth0/address',
  28. '(.*)',
  29. sub { shift },
  30. ],
  31. # MAC address of wireless ethernet connection
  32. WMAC => [
  33. '/sys/class/net/wlan0/address',
  34. '(.*)',
  35. sub { shift },
  36. ],
  37. # Serial number
  38. SERIAL => [
  39. '/proc/cpuinfo',
  40. 'Serial\s+:\s+(\S+)\s*$',
  41. sub { shift },
  42. ],
  43. # Revision id
  44. REV => [
  45. '/proc/cpuinfo',
  46. 'Revision\s+:\s+(\S+)\s*$',
  47. sub { shift },
  48. ],
  49. );
  50. sub CT {
  51. my $item = uc(shift); # not case sensitive
  52. my $value = undef; # result is undef unless success
  53. # if we know how to find the requested item
  54. if ( exists $handles{$item} ) {
  55. # open file
  56. if ( open my $fh, $handles{$item}[0] ) {
  57. my $regex = $handles{$item}[1];
  58. while ( <$fh> ) {
  59. # regex matches: process and set resulting value
  60. /$regex/ and $value = &{$handles{$item}[2]}($1) and last;
  61. }
  62. close $fh;
  63. }
  64. # complain: failed to open file
  65. else {
  66. warn "Could not read $item: $!\n";
  67. }
  68. }
  69. # complain: don't know requested item
  70. else {
  71. warn "Don't know how to find $item\n";
  72. }
  73. return $value;
  74. };
  75. sub CT_Initialize($) {
  76. my ($hash) = @_;
  77. }
  78. 1;