Awk: cmd. line:1: fatal: division by zero attempted


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: cmd. line:1: fatal: division by zero attempted
# 1  
Old 02-21-2013
Awk: cmd. line:1: fatal: division by zero attempted

when i try the snippet in the console its working fine:

Code:
ps awwwux | grep php-fpm | grep -v grep | grep -v master | awk '{total_mem = $6 * 1024 + total_mem; total_proc++} END{printf("%d\n", total_mem / total_proc)}'

output:
Quote:
4553523
but when i try the bash script:
Code:
#!/bin/sh
# -*- sh -*-
#
# Plugin to monitor the average process size of PHP5-FPM on the machine.
#
# Copyright TJ Stein 2010 http://constantshift.com
#
# Parameters:
#
# config (required)
# autoconf (optional - used by munin-config)
#
# Magick markers (optional - used by munin-config and som installation
# scripts):
#%# family=manual
#%# capabilities=autoconf

PHP_BIN="php-fpm"

if [ "$1" = "autoconf" ]; then
echo yes
exit 0
fi

if [ "$1" = "config" ]; then
echo 'graph_title PHP5-FPM Average Process Size'
echo 'graph_args --base 1024 -l 0 '
echo 'graph_vlabel PHP5-FPM Average Process Size'
echo 'graph_category PHP'
echo 'graph_info This graph shows the average process size for PHP5-FPM'
echo 'php_average.label PHP5-FPM Average Proccess Size'
echo 'php_average.draw LINE2'
echo 'php_average.info The average process size for PHP5-FPM'
exit 0
fi

echo -n "php_average.value "
ps awwwux | grep $PHP_BIN | grep -v grep | grep -v master | awk '{total_mem = $6 * 1024 + total_mem; total_proc++} END{printf("%d\n", total_mem / total_proc)}'

then I got the following error:
Quote:
php_average.value awk: cmd. line:1: fatal: division by zero attempted
i'm using ubuntu quantal quetzal. anyone can help me please?
# 2  
Old 02-21-2013
Your code looks good to me. I don't see any reason it should not work in a script but command line except the program: php-fpm might not be running at the time you executed your script.

I suggest to remove grep from pipeline and handle that part in awk itself.

Put a check to verify if total_proc is not equal to zero so that it will not throw an error in the event of program not running:
Code:
ps awwwux | awk -v P="$PHP_BIN" '$0~P&&!/awk/&&!/master/{total_mem=$6*1024+total_mem;total_proc++}END{if(total_proc!=0) printf("%d\n", total_mem / total_proc)}'

# 3  
Old 02-21-2013
i have found the problem. i don't had enough access trough the user "munin" to the program "ps". problem solved. thank you.

Last edited by danieloooo; 02-21-2013 at 12:55 PM..
# 4  
Old 02-21-2013
If you omit some w's you don't need access to the processes ARGV memory.
Code:
ps auxw ...
ps aux ...

And it will become faster.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Fatal division by zero attempted

Hello. I'm writing an awk script that looks at a .csv file and calculates the weighted grade based on the scores and categories in the file. I keep getting a fatal division by zero attempted error and I know what it means but I've been looking over the code for awhile and am not sure what is... (4 Replies)
Discussion started by: Eric7giants
4 Replies

2. Shell Programming and Scripting

Division by zero attempted error during linear conversion of values between 0.25 to 1

I want to implement the below formula with awk oneliner new_value = ((old_value - old_min) / (old_max - old_min) ) * (new_max - new_min) + new_min I want to pass the value of old_min and old_min as variable. Here is what I did for this old_min=$(awk 'BEGIN{a=100000000000}{if ($10<0+a) a=$10}... (2 Replies)
Discussion started by: sammy777888
2 Replies

3. Shell Programming and Scripting

awk Division By Zero Bypass

Hi Friends, I don't understand why "a" is always being printed as zero, when I execute the following command. awk '{if($6||$8||$10||$12==0)a=b=c=d=0;else (a=$5/$6);(b=$7/$8);(c=$9/$10);(d=$11/$12); {print... (6 Replies)
Discussion started by: jacobs.smith
6 Replies

4. Shell Programming and Scripting

awk fatal:division by zero attempted bypass with a condtion

Hi Friends, My input chr1 100 200 1234E-02 0.01 0.05 10 chr1 100 200 14E-11 0.11 0.50 1 chr1 100 200 134E-22 0.00 0.65 111 My command awk '{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$13}' input | awk '{v=($5/$6); print $0"\t"v}' OFS="\t" | awk '{$8=(log($8)/log(2)); print $0}'... (4 Replies)
Discussion started by: jacobs.smith
4 Replies

5. Shell Programming and Scripting

variables division with awk

hello i try to divide 2 variables in order to get a percentage--that's why i'm not interested in integer division--but nothing seems to work I think awk is suitable for this but i'm not quite sure how to use it.. any ideas? here's what I want to do: percentage = varA/varB thank you (2 Replies)
Discussion started by: vlm
2 Replies

6. Shell Programming and Scripting

awk division error - 0

input 0 0 9820373 2069 0 0 11485482 awk '{print ($1/$3) / ($4/$7)}' input error Is there any way to fix this problem ? (25 Replies)
Discussion started by: quincyjones
25 Replies

7. UNIX for Advanced & Expert Users

awk: division by zero

I received error "awk: division by zero" while executing the following statement. SunOS 5.10 Generic_142900-15 sun4us sparc FJSV,GPUZC-M echo 8 | awk 'END {printf ("%d\n",NR/$1 + 0.5);}' file1.lst awk: division by zero Can someone provide solution? Thanks Please use code... (11 Replies)
Discussion started by: kumar77
11 Replies

8. Shell Programming and Scripting

awk error message: division by zero attempted

Hi, I'm executing unixbench tool v4.1 on an embedded system and I'm getting these error messages: Execl Throughput 1 2 3awk: /unixbench/unixbench-4.1.0/pgms/loops.awk:38: (FILENAME=- FNR=4) fatal: division by zero attempted Pipe Throughput 1 2 3 4 5 6 7 8 9 10awk:... (3 Replies)
Discussion started by: rogelio
3 Replies

9. Shell Programming and Scripting

awk Division and modulus

I need to read the file divide 3 column with 2nd and run a modulus of 10 and check whether the remainder is zero or not if not print the entire line. cat filename | awk '{ if ($3 / $2 % 10 != 0) print $0}' Whats wrong with it ? (4 Replies)
Discussion started by: dinjo_jo
4 Replies

10. Shell Programming and Scripting

error "awk: (FILENAME=- FNR=23) fatal: division by zero attempted"

Hi , I have file : after i run this command : there are error can we print blank line if output error ?? thanks.. ^^ (4 Replies)
Discussion started by: justbow
4 Replies
Login or Register to Ask a Question