add a directory for running nagios scale/performance tests
[monitor.git] / nagios / test / common.sh
1 #!/bin/bash 
2
3 function percent_true ()
4 {
5     PERCENT=$1
6
7     # If R is uniformly random, then it will be less than a threshold PERCENT of the time.
8     P=$(( $PERCENT * 32786 / 100 ))
9     R=$RANDOM
10
11     if [ $R -gt $P ] ; then
12         echo "2"
13     else
14         echo "0"
15     fi
16 }
17
18 function random_delay ()
19 {
20     MAX=$1
21
22     R=$RANDOM
23     P=$(( $R * $MAX / 32786 ))
24
25     echo $P
26 }
27
28 function random_sample ()
29 {
30     file=$1
31     length=$(wc -l $file | awk '{print $1}')
32     R=$RANDOM
33     R_MAX=32786
34     index=$(( $R * $length / $R_MAX ))
35
36     V=`tail -$(( $length - $index )) $file  | head -1`
37     echo $V
38 }
39
40 function str_to_state ()
41 {
42     case "$1" in
43         "OK:")
44             echo "0"
45             ;;
46         "WARNING:")
47             echo "1"
48             ;;
49         *)
50             echo "2"
51             ;;
52     esac
53 }
54
55 function open_http ()
56 {
57     exec 3<> /dev/tcp/$1/80
58     echo "GET /index.html HTTP/1.0" 1>&3
59 }
60
61 function close_http ()
62 {
63     echo 1>&3
64     while read 0<&3; do echo $REPLY >/dev/null; done
65 }
66