summaryrefslogtreecommitdiff
path: root/machine-update-beta
blob: 6a4db3e70af29848a4e28ee12dd7c2932ace9cf2 (plain)
  1. #!/usr/bin/perl -w
  2. # For Emacs: -*- mode:cperl; mode:folding; -*-
  3. #
  4. # Get a machine's critical features, And mail/http them to the Linux Counter
  5. #
  6. # (c) 1999 - Harald Tveit Alvestrand, the Linux Counter Project
  7. # 2003 - PetaMem Group (www.petamem.com)
  8. # License: GNU Copyleft - see bottom of file.
  9. # Changelog: see even more bottom of the file
  10. #
  11. # As a matter of courtesy, if you change this file on your own,
  12. # make sure it does NOT mail to the counter!
  13. #
  14. use strict;
  15. use POSIX;
  16. our $VERSION = '0.20';
  17. our $CVS_VERSION = '$Revision: 1.1 $ $Date: 2005-03-11 16:57:22 $ $Author: jonas $';
  18. our $IsInTestHarness;
  19. use vars qw(%values %oldvalues $errordata $debugdata); # data that is sent
  20. use vars qw($progname %option);
  21. use vars qw(%is_sys_account %is_user %is_account);
  22. # Some variables are for internal use, and never prompted for in
  23. # the loop of askquestions
  24. our %dontask = ('uniqueid' => 1, # Internal use
  25. 'manual' => 1,
  26. 'method' => 1,
  27. 'owner' => 1, # These 2 are always prompted for
  28. 'key' => 1,
  29. 'uptime' => 1, # We think we know how to get these
  30. );
  31. # stuff that controls defaults for passwdscan & accounts subroutines
  32. my ($UID_MIN, $UID_MAX, $got_defs) = (100, 65533, '');
  33. # Make sure nothing happens, so that the script's routines
  34. # can be debugged from another file
  35. return 1 if($IsInTestHarness);
  36. &preparation;
  37. &options;
  38. &readfile;
  39. &checkconfig;
  40. if ($option{ask}) {
  41. &askquestions;
  42. } else {
  43. &copymanuals;
  44. }
  45. &writefile;
  46. &sendfile;
  47. # {{{ preparation
  48. #
  49. sub preparation {
  50. die "No HOME environment variable\n" if (!$ENV{HOME});
  51. die "No home diretory\n" if ! -d $ENV{HOME};
  52. my $infodir = "$ENV{HOME}/.linuxcounter";
  53. if (! -d $infodir) {
  54. mkdir($infodir, 0766) || die "Unable to make $infodir\n";
  55. }
  56. # Keep track of where I am; need it to install crontab entry
  57. # progname is a global.
  58. $progname = $0;
  59. if ($progname !~ /^\//) {
  60. my $progdir = `pwd`;
  61. chop $progdir;
  62. $progname = "$progdir/$progname";
  63. $progname =~ s!/./!/!;
  64. }
  65. chdir($infodir) || die "Unable to change to $infodir\n";
  66. my ($sysname, $nodename, $release, $version, $machine ) = POSIX::uname();
  67. if (! -f $nodename) {
  68. print STDERR "Machine-update $VERSION. Use $0 -l to display license.\n";
  69. print STDERR "Creating the infofile for your computer.\n";
  70. # Create the infodir
  71. open(INFO, ">$nodename");
  72. print INFO "uniqueid: ", randomnumber(), "\n";
  73. close INFO;
  74. }
  75. srand time % $$; # do some seed "randomization"
  76. }
  77. # }}}
  78. # {{{ options
  79. #
  80. sub options {
  81. my $opt;
  82. while (defined($ARGV[0]) && $ARGV[0] =~ /^-/) {
  83. $opt = shift @ARGV;
  84. $opt =~ /c/ && &installcrontab;
  85. $opt =~ /d/ && $option{DEBUG}++ && print STDERR "Debug is $option{DEBUG}\n";
  86. $opt =~ /h/ && &help;
  87. $opt =~ /i/ && ($option{ask} = 1);
  88. $opt =~ /l/ && &license;
  89. $opt =~ /m/ && ($option{mail} = 1);
  90. $opt =~ /t/ && ($option{mail} = 0);
  91. $opt =~ /u/ && &uninstallcrontab;
  92. $opt =~ /v/ && die "\n\t Linux Counter machine-update version $VERSION\n"
  93. . "\tCVS version $CVS_VERSION\n";
  94. $opt =~ /x/ && ($option{info} = 1);
  95. }
  96. }
  97. # }}}
  98. # {{{ askquestions
  99. #
  100. sub askquestions {
  101. return if ! -t STDIN || ! -t STDOUT;
  102. $| = 1;
  103. print "Here you can specify some info that the script can't know for itself\n";
  104. $values{owner} = askone("Your Linux Counter reg#, if any", $values{owner});
  105. $values{key} = askone("Your machine's counter reg#, if any", $values{key});
  106. print "Here is what the program has found:\n";
  107. for my $key (keys(%values)) {
  108. if (!$dontask{$key}) {
  109. printf "%-10s%1s: %s\n", $key, $dontask{$key}?"*":" ", $values{$key};
  110. }
  111. }
  112. my $domore = &askone('Do you want to override some of the found values?', 'no');
  113. if ($domore =~ /^Y/i) {
  114. my $manual;
  115. for my $key (keys(%values)) {
  116. next if $dontask{$key};
  117. my $value = &askone($key, $oldvalues{$key}, $values{$key});
  118. if ($values{$key} eq $value) { # go to automatic
  119. &Debug("auto value: $key\n");
  120. } else {
  121. &Debug("still manual value: $key\n");
  122. $manual .= " $key";
  123. }
  124. $values{$key} = $value;
  125. }
  126. $values{manual} = $manual;
  127. } else {
  128. delete $values{manual};
  129. }
  130. }
  131. # }}}
  132. # {{{ askone
  133. #
  134. sub askone {
  135. my $prompt = shift;
  136. my $default = shift;
  137. my $probed = shift;
  138. print $prompt;
  139. if (!defined($default) && defined($probed)) {
  140. $default = $probed;
  141. }
  142. if (defined($default)) {
  143. print " [$default]";
  144. }
  145. if (defined($probed) && $probed ne $default) {
  146. print "(program found $probed)";
  147. }
  148. print ':';
  149. my $ans = <STDIN>;
  150. chop $ans;
  151. &Debug("Answer was $ans\n");
  152. $ans = $default if (!length($ans));
  153. return $ans;
  154. }
  155. # }}}
  156. # {{{ copymanuals
  157. #
  158. sub copymanuals {
  159. my %keeps;
  160. if ($values{manual}) {
  161. %keeps = map {$_ => 1} split(' ', $values{manual});
  162. }
  163. &Debug('Keeping '.join(' ', keys(%keeps))."\n");
  164. for my $key (keys(%keeps)) {
  165. $values{$key} = $oldvalues{$key};
  166. }
  167. }
  168. # }}}
  169. # {{{ readfile
  170. #
  171. sub readfile {
  172. my ($sysname, $nodename, $release, $version, $machine ) = POSIX::uname();
  173. open(INFO, $nodename) || die "Did not find infofile $nodename\n";
  174. while (<INFO>) {
  175. chop;
  176. s/#.*//;
  177. if (/^(\S+): *(.+)/) {
  178. &Debug("Read $1: $2\n");
  179. $values{$1} = $2;
  180. } else {
  181. print STDERR "Unparsed info line: $_ - discarded\n";
  182. }
  183. }
  184. close INFO;
  185. %oldvalues = %values;
  186. }
  187. # }}}
  188. # {{{ writefile
  189. #
  190. sub writefile {
  191. my ($sysname, $nodename, $release, $version, $machine ) = POSIX::uname();
  192. open(INFO, ">$nodename.new");
  193. for my $val (sort keys(%values)) {
  194. &Debug("Saving $val: $values{$val}\n");
  195. print INFO "$val: $values{$val}\n";
  196. }
  197. close INFO;
  198. rename("$nodename.new", $nodename) || die "Rename failed\n";
  199. }
  200. # }}}
  201. # {{{ sendfile
  202. #
  203. sub sendfile {
  204. if ($option{mail}) {
  205. open(MAIL, "|/usr/lib/sendmail machine-registration\@counter.li.org")
  206. || die "Unable to open sendmail\n";
  207. } else {
  208. warn "--------------------------------------------------------\n";
  209. warn "This is what will be sent to the Linux Counter if you\n";
  210. warn "run the program with the -m switch. Now, NOTHING IS SENT\n";
  211. warn "--------------------------------------------------------\n";
  212. open(MAIL, ">&STDOUT");
  213. }
  214. # note that $ENV{USER} isn't (always) set in a cron job...
  215. my $user = (getpwuid($<))[0];
  216. $user = "unknown-id-$<" if !$user;
  217. print MAIL <<EOF
  218. From: $user
  219. To: machine-registration\@counter.li.org
  220. Subject: machine-update for $values{name}
  221. //MACHINE
  222. EOF
  223. ;
  224. for my $val (sort keys(%values)) {
  225. print MAIL "$val: $values{$val}\n"
  226. if length($values{$val}) > 0;
  227. }
  228. print MAIL "//END\n";
  229. # Attach possible other info
  230. if ($errordata) {
  231. print MAIL "----- Problem info gathered during probing -----\n";
  232. print MAIL $errordata;
  233. }
  234. $option{info} && do {
  235. print MAIL "----- Debug data for the script maintainer's aid -----\n";
  236. print MAIL $debugdata;
  237. };
  238. close MAIL;
  239. }
  240. # }}}
  241. # {{{ randomnumber
  242. #
  243. sub randomnumber {
  244. return int(rand(1_000_000_000));
  245. }
  246. # }}}
  247. # {{{ checkconfig
  248. #
  249. sub checkconfig {
  250. my ($sysname, $nodename, $release, $version, $machine ) = POSIX::uname();
  251. warn "This is not Linux, but $sysname!\n" if($sysname ne 'Linux');
  252. $values{method} = "machine-update version $VERSION";
  253. $values{os} = $sysname;
  254. $values{kernel} = $release;
  255. $values{cpu_uname} = $machine;
  256. $values{name} = $nodename; # First order guess
  257. # Credit for some of the code below goes to
  258. # Denis Havlik: <havlik@ap.univie.ac.at>
  259. # Blame is, of course, all mine - HTA -
  260. # Note - there are numerous problems with df, including:
  261. # - early versions don't support the -l option
  262. # - at least some include SAMBA filesystems in the -l option
  263. open (TMP,"df -l -x shm |"); # exclude shmfs, its 50% of RAM (mostly)
  264. my $HD = 0;
  265. while (<TMP>) {
  266. my @line = split(/\s+/);
  267. ($line[0] =~ /\/dev/) && ($HD += $line[1]);
  268. }
  269. &Debug("$HD kbytes of disk found\n");
  270. $HD /= 1024;
  271. $values{disk} = sprintf("%d", $HD);
  272. $values{accounts} = &accounts;
  273. $values{users} = &active_users;
  274. my $uptime = &xbin('uptime');
  275. if($uptime) {
  276. $uptime = `$uptime`;
  277. # get the uptime string here (could be more elegant, but so it is
  278. # robust against various localizations)
  279. if ($uptime =~ /^\s+[^\s]+\s+[^\s]+\s+([^,]+), /) {
  280. $values{uptime} = $1;
  281. } else {
  282. &ErrorInfo("Can't parse uptime output: $uptime\n");
  283. }
  284. if ($option{info}) {
  285. &DebugInfo("***** Uptime output *****\n$uptime");
  286. }
  287. }
  288. # Not sure this is a Right Thing...so not saving it for the moment
  289. # This section based on a patch from Mark-Jason Dominus <mjd@plover.com>
  290. # try to guess mailer based on content of /usr/lib/sendmail link
  291. if (-l '/usr/lib/sendmail') {
  292. my $realsendmail = readlink('/usr/lib/sendmail');
  293. if ($realsendmail eq '../sbin/sendmail') {
  294. $realsendmail = '/usr/sbin/sendmail';
  295. if (-l $realsendmail) {
  296. $realsendmail = readlink($realsendmail);
  297. }
  298. }
  299. if ($realsendmail =~ m{^/var/qmail}) {
  300. $values{mailer} = "qmail";
  301. } else {
  302. &DebugInfo("Found sendmail as a link to $realsendmail\n");
  303. }
  304. }
  305. # Link method did not work. Try to guess based on presence of
  306. # config files. (this is more susceptible to the old-junk problem)
  307. if (!$values{mailer}) {
  308. if ( -d '/var/qmail') {
  309. $values{mailer} = 'qmail';
  310. } elsif ( -f '/etc/sendmail.cf') {
  311. $values{mailer} = 'sendmail';
  312. } elsif ( -d '/etc/postfix') {
  313. $values{mailer} = 'postfix';
  314. }
  315. }
  316. # forget about /proc/kcore: It's not reliable above 960MB. It also doesn't show
  317. # real memory, because some may be eaten by graphics
  318. # forget also about the "free" command: We don't want to be dependant on that
  319. $values{memory} = int(&getval_from_file('/proc/meminfo',1,1) / (1024*1024));
  320. # But actually: Shouldn't we return the total of virtual memory here? Seems to
  321. # be relevant to me - RJ -
  322. &cpuinfo;
  323. }
  324. # }}}
  325. # {{{ cpuinfo
  326. #
  327. sub cpuinfo {
  328. my $cpufile = '/proc/cpuinfo'; # Linux: Place to get info on CPU
  329. my %interesting = (# 2.0 and 2.2 kernels
  330. 'bogomips' => '+bogomips',
  331. 'processor' => '1+processors',
  332. 'vendor_id' => 'cpu_vendor',
  333. # 2.0 kernels
  334. 'cpu' => 'cpu_only',
  335. 'model' => 'cpu_model',
  336. 'model name' => 'cpu_model_name',
  337. # 2.2 kernels
  338. 'cpu MHz' => 'cpu_mhz',
  339. 'cpu family' => 'cpu_family',
  340. # from an Alpha processor
  341. 'cycle frequency [Hz]' => 'cpu_hz',
  342. 'BogoMIPS' => '+bogomips',
  343. 'cpu model' => 'cpu_model',
  344. 'system type' => 'cpu_system_type',
  345. 'cpus detected' => 'processors',
  346. # from a PowerMAC
  347. 'machine' => 'cpu_machine',
  348. 'clock' => 'cpu_clock',
  349. 'motherboard' => 'cpu_motherboard',
  350. # from an UltraSparc
  351. 'BogoMips' => '+bogomips',
  352. 'ncpus active' => 'processors',
  353. );
  354. # Zero out the accumulative values
  355. $values{bogomips} = 0;
  356. $values{processors} = 0;
  357. if (open (TMP,"<$cpufile")) {
  358. &DebugInfo("**** Contents of $cpufile ****\n");
  359. while (<TMP>) {
  360. &DebugInfo($_); # Save /proc/cpuinfo to debugdata if -d
  361. chop;
  362. # A bizarre selection of names are "interesting".
  363. # Make a data-driven pick routine
  364. if (/^(\S+[^:]+\S)\s+: /) {
  365. my $name = $1;
  366. my $value = $';
  367. if ($interesting{$name}) {
  368. if ($interesting{$name} =~ /^\+/) {
  369. $values{$'} += $value;
  370. } elsif ($interesting{$name} =~ /^1\+/) {
  371. $values{$'}++;
  372. } else {
  373. $values{$interesting{$name}} = $value;
  374. }
  375. }
  376. }
  377. }
  378. } else {
  379. &ErrorInfo("Could not open $cpufile\n");
  380. }
  381. }
  382. # }}}
  383. # {{{ accounts
  384. #
  385. sub accounts {
  386. my $s;
  387. my $niss;
  388. my $ypcatbin; # will hold path to the ypcat binary (if any)
  389. open (TMP,"</etc/passwd");
  390. $s += &passwdscan;
  391. &DebugErr("Found $s accounts total\n");
  392. &Debug("Switching to NIS passwords\n");
  393. $ypcatbin = &xbin('ypcat'); # get path to ypcat binary (empty if none)
  394. if($ypcatbin) { # test whether ypcat was found
  395. open TMP, "$ypcatbin passwd 2> /dev/null|"
  396. || ($errordata .= "ypcat failed: $!\n");
  397. $niss = &passwdscan;
  398. $s += $niss;
  399. close TMP;
  400. &Debug("Status of ypcat: $?\n");
  401. &DebugErr("Found $niss accounts in ypcat passwd\n");
  402. }
  403. &DebugErr('Sysaccounts: ', join(' ', keys(%is_sys_account)), "\n");
  404. &DebugErr("Found $s accounts total\n");
  405. return $s;
  406. }
  407. # }}}
  408. # {{{ passwdscan
  409. #
  410. sub passwdscan {
  411. # Code for reading login.defs courtesy of Vassilii Khachaturov
  412. # <vassilii@tarunz.org>
  413. local (*DEFS);
  414. # Try importing UID_MIN and UID_MAX from /etc/login.defs, if possible
  415. # else just assume the above defaults for min and max non-system UID
  416. if (!$got_defs && open (DEFS, '/etc/login.defs')) {
  417. while (<DEFS>) {
  418. if (/^\s*(UID_(?:MIN|MAX))\s+(\d+)/) {
  419. # elegant, but not compatible with "strict refs":
  420. #${ $1 } = $2;
  421. if ($1 eq "UID_MIN") {
  422. $UID_MIN = $2;
  423. } else {
  424. $UID_MAX = $2;
  425. }
  426. &Debug("DEFS match: $1 = $2\n");
  427. }
  428. }
  429. close (DEFS);
  430. $got_defs = 1;
  431. }
  432. &Debug("UID_MIN = $UID_MIN, UID_MAX = $UID_MAX\n");
  433. # I suppose this is as good as it gets -
  434. # Usually user accounts have UID > 100 and
  435. # "system accounts" have UID < 100, but there is no guarantee
  436. # that
  437. # this will hold for pseudo-users like "postgress" etc.
  438. # Also nobody is usually 99 on linux, but -1 on "standard" unices.
  439. # RedHat places the dividing line at 500. Others use 400...
  440. my @line;
  441. my $s = 0;
  442. while (<TMP>) {
  443. @line = split ':';
  444. if ($line[2] >= $UID_MIN && $line[2] <= $UID_MAX
  445. && !($line[0] eq 'nobody')) {
  446. $s++;
  447. $is_account{$line[0]} = 1;
  448. } else {
  449. $is_sys_account{$line[0]} = 1;
  450. }
  451. }
  452. return $s;
  453. }
  454. # }}}
  455. # {{{ active_users
  456. #
  457. # This is kind of alpha, but please test it.
  458. # It calculates the number of "active" users based on the "wtmp" entries
  459. # unfortunately at least Mandrake 8 and 9 ship with non-world-read wtmp
  460. # and non-set-uid last, so this does not work any more...
  461. #
  462. # RJ: Actually I think the best thing to do is to bury this code and be silent about it.
  463. #
  464. sub active_users {
  465. my $userslisted;
  466. for (qw(reboot wtmp runlevel)) { # This sysaccounts shouldn't be counted. Who else?
  467. $is_sys_account{$_} = 1;
  468. }
  469. open( TMP, "/usr/bin/last 2>&1|");
  470. while (<TMP>) {
  471. chop;
  472. if (m!/var/log/wtmp: Permission denied!) { # RJ: ***Boom*** on every non-EN system
  473. &ErrorInfo("/usr/bin/last failed because /var/log/wtmp isn't readable\n");
  474. last;
  475. }
  476. last if(!$_); # RJ: quick hack to safe bad code from harm
  477. my @tmp = split;
  478. my $name = $tmp[0];
  479. if ($is_sys_account{$name}) {
  480. # do nothing
  481. } elsif (defined $is_account{$name}) {
  482. $is_user{$name} = 1;
  483. } elsif (/^\s*$/) { # blank line - do nothing
  484. } elsif ($#tmp == 9) { # OK line, but unknown user
  485. $option{DEBUG} && do {
  486. if (!$userslisted) {
  487. print STDERR 'Know users are: ',
  488. join(' ', keys(%is_account)), "\n";
  489. $userslisted = 1;
  490. }
  491. print STDERR "Unknown user: $name\n";
  492. }
  493. } else {
  494. &DebugErr("Strange line: $_\n");
  495. }
  496. }
  497. close TMP;
  498. my $i = 0;
  499. for (sort keys %is_user) {
  500. $option{DEBUG} && printf "Active user %3d: %s\n", ++$i, $_;
  501. }
  502. &Debug("$i active users found.\n");
  503. return $i;
  504. }
  505. # }}}
  506. # {{{ installcrontab
  507. #
  508. sub installcrontab {
  509. my $hour = int(rand(24));
  510. my $min = int(rand(60));
  511. my $day = int(rand(7)); # Weekday. This version runs once a week.
  512. my $cron;
  513. warn "Installing start of script into your crontab\n";
  514. if (open(CRON, "crontab -l |")) {
  515. &Debug("Checking crontab for machine-update\n");
  516. &Debug("Want to install as $progname\n");
  517. while (<CRON>) {
  518. if (/^#/ && $. <= 3) { # initial comment
  519. &Debug("Skipping comment: $_");
  520. next;
  521. }
  522. if (/machine-update/) {
  523. if (/ $progname -m/) {
  524. die "Crontab entry already installed: $_\n";
  525. } else {
  526. die "Another entry with machine-update: $_\n";
  527. }
  528. }
  529. $cron .= $_;
  530. }
  531. close CRON;
  532. &Debug("Result from crontab -l: ", $? / 256, "\n");
  533. if ($? == 0) {
  534. &Debug("Crontab successfully read\n");
  535. } elsif ($? == 256) {
  536. warn "You don't seem to have a crontab. I will create one.\n";
  537. } else {
  538. die "Failed to read your crontab. Please report this as a bug: $?\n";
  539. }
  540. } else {
  541. &Debug("Result from crontab open(): $?\n");
  542. die "Unable to execute crontab command. Please check your system\n";
  543. }
  544. open(CRON, "|crontab -");
  545. print CRON $cron;
  546. print CRON "$min $hour * * $day $progname -m\n";
  547. close CRON;
  548. &Debug("Result from crontab: $?\n");
  549. if ($?) {
  550. die(<<EoF);
  551. Installing new crontab failed.
  552. YOUR CRONTAB MAY BE DAMAGED - use crontab -l to check it.
  553. Here's its former content (if any):
  554. $cron
  555. EoF
  556. }
  557. print "Crontab entry successfully installed.\nWill run on day $day of every week, at $hour:$min\n";
  558. exit 0;
  559. }
  560. # }}}
  561. # {{{ uninstallcrontab
  562. #
  563. sub uninstallcrontab {
  564. my $found = 0;
  565. my $cron;
  566. print STDERR "Removing $progname from your crontab\n";
  567. open(CRON, "crontab -l |");
  568. &Debug("Checking crontab for machine-update\n");
  569. &Debug("Want to uninstall as $progname\n");
  570. while (<CRON>) {
  571. if (/^#/ && $. <= 3) { # initial comment
  572. &Debug("Skipping comment: $_");
  573. next;
  574. }
  575. if (/machine-update/) {
  576. if (/ $progname -m/) {
  577. print STDERR "Crontab entry found and removed\n";
  578. $found = 1;
  579. next; # skip stuff at end....
  580. } else {
  581. die "Another entry with machine-update: $_\nUninstall manually?\n";
  582. }
  583. }
  584. $cron .= $_;
  585. }
  586. close CRON;
  587. &Debug("Result from crontab -l: $?\n");
  588. if ($?) {
  589. die "Failed to read your crontab. You may not have one?\n";
  590. }
  591. if ($found) {
  592. open(CRON, "|crontab -");
  593. print CRON $cron;
  594. close CRON;
  595. &Debug("Result from crontab: $?\n");
  596. if ($?) {
  597. die(<<EoF);
  598. Installing new crontab failed.
  599. YOUR CRONTAB MAY BE DAMAGED - use crontab -l to check it.
  600. Here's its former content (if any):
  601. $cron
  602. EoF
  603. }
  604. } else {
  605. print STDERR "No instance of $progname found in your crontab\n";
  606. }
  607. exit 0;
  608. }
  609. # }}}
  610. # {{{ xbin execute a linux binary
  611. #
  612. # This sub is to execute a linux binary robustly. i.e. testing
  613. # whether it is present, where it is present, whether it is executable
  614. #
  615. sub xbin {
  616. my $bin = shift; # get name of binary to execute
  617. $bin = `which $bin`; # determine binarys full path
  618. chomp $bin;
  619. return $bin if(-x $bin); # if there and executable: all is well - return it
  620. if(!$bin) { # if not there
  621. &Debug("No $bin found\n"); # state so
  622. } else { # there but not executable
  623. &Debug("$bin found, but not executable\n");
  624. }
  625. return ''; # so return an empty string (binary will not exec)
  626. }
  627. # }}}
  628. # {{{ getval_from_file get value from system file @ row,col
  629. #
  630. sub getval_from_file {
  631. my $file = shift;
  632. my $row = shift;
  633. my $col = shift;
  634. my @file;
  635. my @cols;
  636. if (!(-r $file)) {
  637. &DebugErr("File $file not readable\n");
  638. return '';
  639. }
  640. sysopen(FH,$file, O_RDONLY);
  641. @file = <FH>; # read whole file to array
  642. close FH;
  643. @cols = split /\s+/, $file[$row]; # get the right row
  644. return $cols[$col]; # return the right column
  645. }
  646. # }}}
  647. # {{{ Debug print debug information if flag is set
  648. #
  649. sub Debug {
  650. $option{DEBUG} && print @_;
  651. }
  652. # }}}
  653. # {{{ DebugErr print debug on STDERR if flag is set
  654. #
  655. sub DebugErr {
  656. $option{DEBUG} && print STDERR @_;
  657. }
  658. # }}}
  659. # {{{ ErrorInfo
  660. sub ErrorInfo {
  661. $errordata .= join('', @_);
  662. }
  663. # }}}
  664. # {{{ DebugInfo
  665. sub DebugInfo {
  666. $option{info} && ($debugdata .= join('', @_));
  667. }
  668. # }}}
  669. # {{{ help print help & exit
  670. #
  671. sub help {
  672. my $host = `uname -n`;
  673. print <<EoF;
  674. machine-update version $VERSION
  675. Send machine information to the Linux Counter
  676. USE: machine-update [-i] [-(t|d|l|m|v|x|c|u|h)]
  677. SWITCHES:
  678. -i = interactive
  679. -t = test (do not send e-mail, just print it ot STDOUT - default)
  680. -d = debug (test, and print additional debug informations)
  681. -l = display license
  682. -m = mail results to linux-counter
  683. -v = print version and exit
  684. -x = send extra info to server (Debug)
  685. -c = install crontab entry
  686. -u = uninstall crontab entry
  687. -h = print usage information and exit
  688. If called interactively, will ask some questions and store the
  689. answers in $ENV{HOME}/.linuxcounter/$host
  690. EoF
  691. exit 0;
  692. }
  693. # }}}
  694. # {{{ license print license & exit
  695. #
  696. sub license {
  697. print <<EoF;
  698. Linux Counter Machine Update version $VERSION
  699. Copyright (C) 1999 Harald Tveit Alvestrand
  700. 2003 PetaMem Group (www.petamem.com)
  701. This program is free software; you can redistribute it and/or modify
  702. it under the terms of the GNU General Public License as published by
  703. the Free Software Foundation; either version 2 of the License, or
  704. (at your option) any later version.
  705. This program is distributed in the hope that it will be useful,
  706. but WITHOUT ANY WARRANTY; without even the implied warranty of
  707. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  708. GNU General Public License below for more details.
  709. GNU GENERAL PUBLIC LICENSE
  710. Version 2, June 1991
  711. Copyright (C) 1989, 1991 Free Software Foundation, Inc.
  712. 675 Mass Ave, Cambridge, MA 02139, USA
  713. Everyone is permitted to copy and distribute verbatim copies
  714. of this license document, but changing it is not allowed.
  715. Preamble
  716. The licenses for most software are designed to take away your
  717. freedom to share and change it. By contrast, the GNU General Public
  718. License is intended to guarantee your freedom to share and change free
  719. software--to make sure the software is free for all its users. This
  720. General Public License applies to most of the Free Software
  721. Foundation's software and to any other program whose authors commit to
  722. using it. (Some other Free Software Foundation software is covered by
  723. the GNU Library General Public License instead.) You can apply it to
  724. your programs, too.
  725. When we speak of free software, we are referring to freedom, not
  726. price. Our General Public Licenses are designed to make sure that you
  727. have the freedom to distribute copies of free software (and charge for
  728. this service if you wish), that you receive source code or can get it
  729. if you want it, that you can change the software or use pieces of it
  730. in new free programs; and that you know you can do these things.
  731. To protect your rights, we need to make restrictions that forbid
  732. anyone to deny you these rights or to ask you to surrender the rights.
  733. These restrictions translate to certain responsibilities for you if you
  734. distribute copies of the software, or if you modify it.
  735. For example, if you distribute copies of such a program, whether
  736. gratis or for a fee, you must give the recipients all the rights that
  737. you have. You must make sure that they, too, receive or can get the
  738. source code. And you must show them these terms so they know their
  739. rights.
  740. We protect your rights with two steps: (1) copyright the software, and
  741. (2) offer you this license which gives you legal permission to copy,
  742. distribute and/or modify the software.
  743. Also, for each author's protection and ours, we want to make certain
  744. that everyone understands that there is no warranty for this free
  745. software. If the software is modified by someone else and passed on, we
  746. want its recipients to know that what they have is not the original, so
  747. that any problems introduced by others will not reflect on the original
  748. authors' reputations.
  749. Finally, any free program is threatened constantly by software
  750. patents. We wish to avoid the danger that redistributors of a free
  751. program will individually obtain patent licenses, in effect making the
  752. program proprietary. To prevent this, we have made it clear that any
  753. patent must be licensed for everyone's free use or not licensed at all.
  754. The precise terms and conditions for copying, distribution and
  755. modification follow.
  756. GNU GENERAL PUBLIC LICENSE
  757. TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
  758. 0. This License applies to any program or other work which contains
  759. a notice placed by the copyright holder saying it may be distributed
  760. under the terms of this General Public License. The "Program", below,
  761. refers to any such program or work, and a "work based on the Program"
  762. means either the Program or any derivative work under copyright law:
  763. that is to say, a work containing the Program or a portion of it,
  764. either verbatim or with modifications and/or translated into another
  765. language. (Hereinafter, translation is included without limitation in
  766. the term "modification".) Each licensee is addressed as "you".
  767. Activities other than copying, distribution and modification are not
  768. covered by this License; they are outside its scope. The act of
  769. running the Program is not restricted, and the output from the Program
  770. is covered only if its contents constitute a work based on the
  771. Program (independent of having been made by running the Program).
  772. Whether that is true depends on what the Program does.
  773. 1. You may copy and distribute verbatim copies of the Program's
  774. source code as you receive it, in any medium, provided that you
  775. conspicuously and appropriately publish on each copy an appropriate
  776. copyright notice and disclaimer of warranty; keep intact all the
  777. notices that refer to this License and to the absence of any warranty;
  778. and give any other recipients of the Program a copy of this License
  779. along with the Program.
  780. You may charge a fee for the physical act of transferring a copy, and
  781. you may at your option offer warranty protection in exchange for a fee.
  782. 2. You may modify your copy or copies of the Program or any portion
  783. of it, thus forming a work based on the Program, and copy and
  784. distribute such modifications or work under the terms of Section 1
  785. above, provided that you also meet all of these conditions:
  786. a) You must cause the modified files to carry prominent notices
  787. stating that you changed the files and the date of any change.
  788. b) You must cause any work that you distribute or publish, that in
  789. whole or in part contains or is derived from the Program or any
  790. part thereof, to be licensed as a whole at no charge to all third
  791. parties under the terms of this License.
  792. c) If the modified program normally reads commands interactively
  793. when run, you must cause it, when started running for such
  794. interactive use in the most ordinary way, to print or display an
  795. announcement including an appropriate copyright notice and a
  796. notice that there is no warranty (or else, saying that you provide
  797. a warranty) and that users may redistribute the program under
  798. these conditions, and telling the user how to view a copy of this
  799. License. (Exception: if the Program itself is interactive but
  800. does not normally print such an announcement, your work based on
  801. the Program is not required to print an announcement.)
  802. These requirements apply to the modified work as a whole. If
  803. identifiable sections of that work are not derived from the Program,
  804. and can be reasonably considered independent and separate works in
  805. themselves, then this License, and its terms, do not apply to those
  806. sections when you distribute them as separate works. But when you
  807. distribute the same sections as part of a whole which is a work based
  808. on the Program, the distribution of the whole must be on the terms of
  809. this License, whose permissions for other licensees extend to the
  810. entire whole, and thus to each and every part regardless of who wrote it.
  811. Thus, it is not the intent of this section to claim rights or contest
  812. your rights to work written entirely by you; rather, the intent is to
  813. exercise the right to control the distribution of derivative or
  814. collective works based on the Program.
  815. In addition, mere aggregation of another work not based on the Program
  816. with the Program (or with a work based on the Program) on a volume of
  817. a storage or distribution medium does not bring the other work under
  818. the scope of this License.
  819. 3. You may copy and distribute the Program (or a work based on it,
  820. under Section 2) in object code or executable form under the terms of
  821. Sections 1 and 2 above provided that you also do one of the following:
  822. a) Accompany it with the complete corresponding machine-readable
  823. source code, which must be distributed under the terms of Sections
  824. 1 and 2 above on a medium customarily used for software interchange; or,
  825. b) Accompany it with a written offer, valid for at least three
  826. years, to give any third party, for a charge no more than your
  827. cost of physically performing source distribution, a complete
  828. machine-readable copy of the corresponding source code, to be
  829. distributed under the terms of Sections 1 and 2 above on a medium
  830. customarily used for software interchange; or,
  831. c) Accompany it with the information you received as to the offer
  832. to distribute corresponding source code. (This alternative is
  833. allowed only for noncommercial distribution and only if you
  834. received the program in object code or executable form with such
  835. an offer, in accord with Subsection b above.)
  836. The source code for a work means the preferred form of the work for
  837. making modifications to it. For an executable work, complete source
  838. code means all the source code for all modules it contains, plus any
  839. associated interface definition files, plus the scripts used to
  840. control compilation and installation of the executable. However, as a
  841. special exception, the source code distributed need not include
  842. anything that is normally distributed (in either source or binary
  843. form) with the major components (compiler, kernel, and so on) of the
  844. operating system on which the executable runs, unless that component
  845. itself accompanies the executable.
  846. If distribution of executable or object code is made by offering
  847. access to copy from a designated place, then offering equivalent
  848. access to copy the source code from the same place counts as
  849. distribution of the source code, even though third parties are not
  850. compelled to copy the source along with the object code.
  851. 4. You may not copy, modify, sublicense, or distribute the Program
  852. except as expressly provided under this License. Any attempt
  853. otherwise to copy, modify, sublicense or distribute the Program is
  854. void, and will automatically terminate your rights under this License.
  855. However, parties who have received copies, or rights, from you under
  856. this License will not have their licenses terminated so long as such
  857. parties remain in full compliance.
  858. 5. You are not required to accept this License, since you have not
  859. signed it. However, nothing else grants you permission to modify or
  860. distribute the Program or its derivative works. These actions are
  861. prohibited by law if you do not accept this License. Therefore, by
  862. modifying or distributing the Program (or any work based on the
  863. Program), you indicate your acceptance of this License to do so, and
  864. all its terms and conditions for copying, distributing or modifying
  865. the Program or works based on it.
  866. 6. Each time you redistribute the Program (or any work based on the
  867. Program), the recipient automatically receives a license from the
  868. original licensor to copy, distribute or modify the Program subject to
  869. these terms and conditions. You may not impose any further
  870. restrictions on the recipients' exercise of the rights granted herein.
  871. You are not responsible for enforcing compliance by third parties to
  872. this License.
  873. 7. If, as a consequence of a court judgment or allegation of patent
  874. infringement or for any other reason (not limited to patent issues),
  875. conditions are imposed on you (whether by court order, agreement or
  876. otherwise) that contradict the conditions of this License, they do not
  877. excuse you from the conditions of this License. If you cannot
  878. distribute so as to satisfy simultaneously your obligations under this
  879. License and any other pertinent obligations, then as a consequence you
  880. may not distribute the Program at all. For example, if a patent
  881. license would not permit royalty-free redistribution of the Program by
  882. all those who receive copies directly or indirectly through you, then
  883. the only way you could satisfy both it and this License would be to
  884. refrain entirely from distribution of the Program.
  885. If any portion of this section is held invalid or unenforceable under
  886. any particular circumstance, the balance of the section is intended to
  887. apply and the section as a whole is intended to apply in other
  888. circumstances.
  889. It is not the purpose of this section to induce you to infringe any
  890. patents or other property right claims or to contest validity of any
  891. such claims; this section has the sole purpose of protecting the
  892. integrity of the free software distribution system, which is
  893. implemented by public license practices. Many people have made
  894. generous contributions to the wide range of software distributed
  895. through that system in reliance on consistent application of that
  896. system; it is up to the author/donor to decide if he or she is willing
  897. to distribute software through any other system and a licensee cannot
  898. impose that choice.
  899. This section is intended to make thoroughly clear what is believed to
  900. be a consequence of the rest of this License.
  901. 8. If the distribution and/or use of the Program is restricted in
  902. certain countries either by patents or by copyrighted interfaces, the
  903. original copyright holder who places the Program under this License
  904. may add an explicit geographical distribution limitation excluding
  905. those countries, so that distribution is permitted only in or among
  906. countries not thus excluded. In such case, this License incorporates
  907. the limitation as if written in the body of this License.
  908. 9. The Free Software Foundation may publish revised and/or new versions
  909. of the General Public License from time to time. Such new versions will
  910. be similar in spirit to the present version, but may differ in detail to
  911. address new problems or concerns.
  912. Each version is given a distinguishing version number. If the Program
  913. specifies a version number of this License which applies to it and "any
  914. later version", you have the option of following the terms and conditions
  915. either of that version or of any later version published by the Free
  916. Software Foundation. If the Program does not specify a version number of
  917. this License, you may choose any version ever published by the Free Software
  918. Foundation.
  919. 10. If you wish to incorporate parts of the Program into other free
  920. programs whose distribution conditions are different, write to the author
  921. to ask for permission. For software which is copyrighted by the Free
  922. Software Foundation, write to the Free Software Foundation; we sometimes
  923. make exceptions for this. Our decision will be guided by the two goals
  924. of preserving the free status of all derivatives of our free software and
  925. of promoting the sharing and reuse of software generally.
  926. NO WARRANTY
  927. 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
  928. FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
  929. OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
  930. PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
  931. OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  932. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
  933. TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
  934. PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
  935. REPAIR OR CORRECTION.
  936. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  937. WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
  938. REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
  939. INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
  940. OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
  941. TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
  942. YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
  943. PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
  944. POSSIBILITY OF SUCH DAMAGES.
  945. END OF TERMS AND CONDITIONS
  946. EoF
  947. exit 0;
  948. }
  949. # }}}
  950. # Changelog for 0.2
  951. # - indentation and folding marks
  952. # - made script work with -w and use strict
  953. # - removed some localization traps
  954. # - marked some BUGS - but they`re still there (mostly localization)
  955. # - more robust binary calls
  956. # - getval_from_file data acquisition method
  957. # - fixed df (shmfs) - but only temporarily (quick hack)
  958. # - various code optimizations & cleanup (removed unneded vars)
  959. # - Memory size detection now robst and >960MB capable
  960. # - slightly better randomness
  961. #
  962. #vim:ts=8:sw=4:sts=4