Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Some help with my project:

by 0uts1de (Initiate)
on Jan 16, 2017 at 19:01 UTC ( [id://1179690]=perlquestion: print w/replies, xml ) Need Help??

0uts1de has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,i am quite new in perl programming (only 2 weeks learning)and i have a question about my script, the idea is that the script prints some things about a choosen filesystem and gives the avalible space, the used space, the type, etc.

All sounds good, but when I run my script it returns a error ("Illegal division by zero at analizer (16-1-17).pl line 35, <STDIN> line 1.") and I dont know how to fix it or what is working bad, the code is this: (NOTE: I am spanish so i write the comments in my lenguaje, sorry if u dont understand it but i dont think is necesary a traduction)

#!usr/bin/perl #solo funciona para unix/linux/solaris(solaris ni idea) #si da el error "Undefined subroutine &main::SYS_statfs called at +Filesys/DiskSpace.pm at line XXX", editar syscall.ph # (en el perl lib tree) y copiar la linea "SYS_statfs {196;}" fuer +a del "if (defined &__hp9000s800)" (alrededor de la linea 356) use strict ; use warnings ; use Filesys::DiskSpace ; use POSIX ; #sistema de archivos/particion a comprobar espacio #sacar fecha ; my $TIME = strftime("%d/%m/%Y %H:%M:%S", localtime(time) ) ; print "+----------+---+----------+\n" ; print "+Script made up by 0uts1de+\n" ; print "+----------+---+----------+\n" ; print "Today is $TIME \n" ; print "Write the filesystem/partiton you want to monitor, for exam +ple /home or /usr \n" ; my $monit = <STDIN> ; chomp $monit ; print "##warning level is by default about a 20 % of free space, i +f u want to change this##\n" ; print "##change the variable warn in the script with a editor like + vim or gedit ##\n" ; ########## warn string ############ my $warn = 10 ; ################################### #consegir caracteristicas de la particion/filesystem (raw) my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = df $moni +t ; my $monit_percentual = ( $avail / $avail+$used ) * 100 ; my $final_used = $used / 1000 ; my $final_avail = $avail / 1000 ; print "$monit is a $fs_type filesystem who has $final_used space u +sed (in MB) and $final_avail of avalible space (in MB too) \n" ; #comparacion con el porcentaje de $monit_percentual if ($monit_percentual < $warn) { print "Less of the $warn % of the space of the filesystem +$monit is avalible for use, consider uninstall unused software \n" ; } elsif ($monit_percentual eq $warn) { print "Be carefull, exactly the $warn % of the space of $m +onit is usable, consider uninstall unused software \n" ; } else { print "There is a $monit_percentual % of avarible space of + the disk \n" ; }

In the imput I have the error and the rest, here is:

+----------+---+----------+ +Script made up by 0uts1de+ +----------+---+----------+ Today is 16/01/2017 16:31:04 Write the filesystem/partiton you want to monitor, for example /home o +r /usr / ##warning level is by default about a 20 % of free space, if u want to + change this## ##change the variable warn in the script with a editor like vim or ged +it ## Illegal division by zero at analizador de filesystem (16-1-17).pl line + 35, <STDIN> line 1.

Thanks in advance!

Replies are listed 'Best First'.
Re: Some help with my project:
by toolic (Bishop) on Jan 16, 2017 at 19:16 UTC
      That means ($avail+$used)=0
      Due to precedence, it actually means $avail is zero. The OP probably meant to write
      my $monit_percentual = ( $avail / ($avail+$used) ) * 100 ;

      Dave.

Re: Some help with my project:
by 0uts1de (Initiate) on Jan 17, 2017 at 09:39 UTC

    I read both comments and the problem was that the module FileSys::DiskSpace is quite old and abandoned as sayed toolic, and is not able to read modern filesystems as ext4 or ext3, i am using now the Sys::Filesystem module to do the same work, and for the moment is working.

    Thanks both for the replies

      In this code also if you add one more if condition before doing this calculation,
      it will prevent these kind of error if future
      if ($used){ my $monit_percentual = ( $avail / $avail+$used ) * 100 ; my $final_used = $used / 1000 ; my $final_avail = $avail / 1000 ; print "$monit is a $fs_type filesystem who has $final_used space u +sed (in MB) and $final_avail of avalible space (in MB too) \n" ; #comparacion con el porcentaje de $monit_percentual if ($monit_percentual < $warn) { print "Less of the $warn % of the space of the filesystem +$monit is avalible for use, consider uninstall unused software \n" ; } elsif ($monit_percentual eq $warn) { print "Be carefull, exactly the $warn % of the space of $m +onit is usable, consider uninstall unused software \n" ; } else { print "There is a $monit_percentual % of avarible space of + the disk \n" ; } }else{ print "Some message\n"; }
        my $monit_percentual = (  $avail  /  $avail+$used  ) * 100 ;

        "it will prevent these kind of error if future"

        If you want to prevent errors be aware of perlop "Operator precedence and associativity".

        use strict; use warnings; my $avail = 1; my $used = 1; my $testa = ( $avail / $avail + $used ) * 100; my $testb = ( $avail / ( $avail + $used ) ) * 100; print "testa: $testa\ntestb: $testb\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1179690]
Approved by toolic
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 22:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found