ctdb-tests: statd-callout script setup modularisation
[metze/samba/wip.git] / ctdb / tests / eventscripts / stubs / netstat
1 #!/bin/bash
2
3 prog="netstat"
4
5 # Pretty that we're the shell and that this command could not be
6 # found.
7 if [ "$FAKE_NETSTAT_NOT_FOUND" = "yes" ] ; then
8     echo "sh: ${prog}: command not found" >&2
9     exit 127
10 fi
11
12 usage ()
13 {
14     cat >&2 <<EOF
15 Usage: $prog [ -t | --unix ] [ -n ] [ -a ] [ -l ]
16
17 A fake netstat stub that prints items depending on the variables
18 FAKE_NETSTAT_TCP_ESTABLISHED, FAKE_TCP_LISTEN,
19 FAKE_NETSTAT_UNIX_LISTEN, depending on command-line options.
20
21 Note that -n is ignored.
22
23 EOF
24     exit 1
25 }
26
27 # Defaults.
28 tcp=false
29 unix=false
30 all=false
31 listen=false
32
33 parse_options ()
34 {
35     # $POSIXLY_CORRECT means that the command passed to onnode can
36     # take options and getopt won't reorder things to make them
37     # options to this script.
38     _temp=$(POSIXLY_CORRECT=1 getopt -n "$prog" -o "tnalh" -l unix -l help -- "$@")
39
40     [ $? != 0 ] && usage
41
42     eval set -- "$_temp"
43
44     while true ; do
45         case "$1" in
46             -n) shift ;;
47             -a) all=true ; shift ;;
48             -t) tcp=true ; shift ;;
49             -l) listen=true ; shift ;;
50             --unix) unix=true ; shift ;;
51             --) shift ; break ;;
52             -h|--help|*) usage ;; # * shouldn't happen, so this is reasonable.
53         esac
54     done
55
56     [ $# -gt 0 ] && usage
57
58     # If neither -t or --unix specified then print all.
59     $tcp || $unix || { tcp=true ; unix=true ; }
60 }
61
62 parse_options "$@"
63
64 if $tcp ; then
65     if $listen ; then
66         echo "Active Internet connections (servers only)"
67     elif $all ; then
68         echo "Active Internet connections (servers and established)"
69     else
70         echo "Active Internet connections (w/o servers)"
71     fi
72
73     echo "Proto Recv-Q Send-Q Local Address           Foreign Address         State"
74
75     tcp_fmt="tcp        0      0 %-23s %-23s %s\n"
76     for i in $FAKE_NETSTAT_TCP_ESTABLISHED ; do
77         src="${i%|*}"
78         dst="${i#*|}"
79         printf "$tcp_fmt" $src $dst "ESTABLISHED"
80     done
81     while read src dst ; do
82         printf "$tcp_fmt" $src $dst "ESTABLISHED"
83     done <"$FAKE_NETSTAT_TCP_ESTABLISHED_FILE"
84
85     if $all || $listen ; then
86         for i in $FAKE_TCP_LISTEN ; do
87             printf "$tcp_fmt" $i "0.0.0.0:*" "LISTEN"
88         done
89     fi
90 fi
91
92 if $unix ; then
93     if $listen ; then
94         echo "Active UNIX domain sockets (servers only)"
95     elif $all ; then
96         echo "Active UNIX domain sockets (servers and established)"
97     else
98         echo "Active UNIX domain sockets (w/o servers)"
99     fi
100     
101     echo "Proto RefCnt Flags       Type       State         I-Node   Path"
102
103     unix_fmt="unix  2      [ ACC ]     STREAM     LISTENING     %-8d %s\n"
104     if $all || $listen ; then
105         for i in $FAKE_NETSTAT_UNIX_LISTEN ; do
106             printf "$unix_fmt" 12345 "$i"
107         done
108     fi
109 fi