From ca4d4a0051aba1852e73d11033d76073b3b404ef Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Sat, 24 Jun 2006 11:05:33 +0000 Subject: Retry gathering data git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@147 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede --- munin/owfs_temperature_ | 70 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 53 insertions(+), 17 deletions(-) (limited to 'munin/owfs_temperature_') diff --git a/munin/owfs_temperature_ b/munin/owfs_temperature_ index f797b21..8713990 100755 --- a/munin/owfs_temperature_ +++ b/munin/owfs_temperature_ @@ -74,32 +74,68 @@ def config puts "#{n}.label #{label}" end + +def runcmd(command, input) + rdin , wrin = IO.pipe + rdout, wrout = IO.pipe + rderr, wrerr = IO.pipe + + pid = fork + unless pid + # child + wrin.close + rdout.close + rderr.close + STDIN.reopen rdin + STDOUT.reopen wrout + STDERR.reopen wrerr + exec(*command) + throw("fell through exec(). WTF.") + end + rdin.close + wrout.close + wrerr.close + + out = [] + err = [] + tin = Thread.new { wrin.print input; wrin.close } + tout = Thread.new { out = rdout.readlines } + terr = Thread.new { err = rderr.readlines } + tin.join + tout.join + terr.join + Process.wait pid + + exitstatus = $?.exitstatus + + [exitstatus, out, err] +end + def report device = query_device Process.gid=0 Process.egid=0 - temp = nil # fuse does weird checks. this fails: # File.new($owfs_path+'/'+device+'/temperature', "r") ---> FAILS: in `initialize': Permission denied - /var/lib/owfs/10.D234EE000800/temperature (Errno::EACCES) - IO.popen("-") do |f| - unless f # child - begin - exec('cat', $owfs_path+'/'+device+'/temperature') - rescue => e - puts "Cannot exec cat: "+e.message - exit 1 - end - end - temp = f.readlines - end - if $? != 0 - STDERR.puts "Child exited with non-zero exit code(%d): %s"%[$? >> 8, temp.join('')] - else - n = normalize_sensor device - puts "#{n}.value #{temp.join('')}" + # Sometimes the directory in OWFS does not exist, and cat will say + # cat: /var/lib/owfs/10.D234EE000800/temperature: Invalid argument + # so retry it a few times, since ls'ing the directory helps on the command line at least + 5.times do + exitstatus, out, err = runcmd(['cat', $owfs_path+'/'+device+'/temperature'], '') + if exitstatus != 0 + STDERR.puts "Child exited with non-zero exit code(%d): %s"%[exitstatus >> 8, err.join] + runcmd(['ls', $owfs_path]) + sleep 1 + else + STDERR.puts "command succeeded but returned something on stderr: #{err.join}" if err.size > 0 + n = normalize_sensor device + puts "#{n}.value #{out.join}" + return + end end + STDERR.puts "Could not get data." end -- cgit v1.2.3