-
Notifications
You must be signed in to change notification settings - Fork 0
/
dirIO.sh
147 lines (120 loc) · 4.74 KB
/
dirIO.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/bin/bash
# version 0.1
# June 21, 2024
# gpt 3.x
# initial prompt command:
# Please create a code example for data input output monitoring and data rate output within a bash shell command line. Create this script as bash shell script. Create this script for file system data input and data output and data rates from or to this directory, that is declared with script variables on startup. Please add request for keyboard input for stopping that script on pressing q or Q. Add another keyboard input scan for pausing output with pressing p and resuming with space key.
# Check if directory is provided
avail=$( [ ! -d $1 ] || [ -z "$1" ] && echo "0" || echo "1" )
if [ "$1" == "/" ]; then avail=0; echo; echo "*** no root fs io monitoring recommended ***"; fi
echo " \$# $# \$1 $1 directory path available $avail"
keysdef=\
" \n"\
" keys: on 'statx' errors == 'n' \n"\
" pause == 'p' \n"\
" resume == ' ' or 'r' \n"\
" quit == 'q' or 'Q' \n"\
" \n"\
" version 0.1 \n"\
" June 15, 2024 \n"
if [ -z "$1" ] || [ ! "$avail" == 1 ] || [ "$#" -ne 1 ]; then
echo "Usage: $0 /directory/to/monitor"
if [ "$1" == "-h" ] || [ "$1" == "--help" ] || [ "$1" == "-?" ] || [ "$1" == "/?" ]; then
echo -e -n "$keysdef"
fi
echo
exit 1
fi
# Initialize variables
directory="$1"
total_input=0
total_output=0
start_time=$(date +%s)
paused=false
err=false
dir_size=$(find "$directory" -type f,d | xargs stat --format="%s" | awk '{s+=$1} END {print s}')
dir_size_du=$(du -sb "$directory" | awk '{print $1}')
start_dir_size=$((dir_size_du))
current_dir_size=$((dir_size))
start_date=$(date)
echo "monitoring start: $start_date"
echo "directory size (find -type cmd) $((dir_size/1024)) kB, directory size (du cmd) $((dir_size_du/1024)) kB"
echo
sleep 1.0
# Function to monitor data I/O and data rates
monitor_io() {
local rate_in=0
local rate_out=0
# Get current I/O stats
current_io=$(du -sb "$directory" | awk '{print $1}')
# Calculate data rates
# 1s timeout on read cmd
rate_io=$((current_io - dir_size_du))
if [ $((rate_io)) -le 0 ]; then
rate_in=$(( rate_in+rate_io ))
sum_in=$(( sum_in+rate_in ))
else
rate_out=$(( rate_out+rate_io ))
sum_out=$(( sum_out+rate_out ))
fi
# Update previous values
dir_size_du=$((current_io))
# Display data rates
echo " Data Input Rate: $rate_in bytes/sec $((rate_in/1024)) kB/s $((rate_in/(1024*1024))) MB/s"
echo " Data Output Rate: $rate_out bytes/sec $((rate_out/1024)) kB/s $((rate_out/(1024*1024))) MB/s"
echo " Data Input Sum: $sum_in bytes $((sum_in/1024)) kB $((sum_in/(1024*1024))) MB"
echo " Data Output Sum: $sum_out bytes $((sum_out/1024)) kB $((sum_out/(1024*1024))) MB"
echo
}
# Function to calculate data rate output
calculate_data_rate() {
echo "$start_date start_dir_size $((start_dir_size/1024)) kB current_dir_size $((current_dir_size/1024)) kB monitoring time io $(( ($current_dir_size-$start_dir_size)/1024 )) kb"
current_dir_size=$(find "$directory" -type f,d | xargs stat --format="%s" | awk '{s+=$1} END {print s}')
data_rate_output=$((current_dir_size - dir_size))
echo " data_rate_io $data_rate_output B/s"
dir_size=$((current_dir_size))
if [ $((data_rate_output)) -le 0 ]; then
input_sum=$(( input_sum+data_rate_output ))
in_sum_float=`echo "scale=3; $((input_sum))/(1024*1024)" | bc`
else
output_sum=$(( output_sum+data_rate_output ))
out_sum_float=`echo "scale=3; $((output_sum))/(1024*1024)" | bc`
fi
echo " Data rate io: $data_rate_output bytes/s `echo \"scale=4; $data_rate_output/1024/1024\" | bc` MB/s"
echo " data io sum: $((input_sum)) $((output_sum)) bytes"
echo " data io sum: $in_sum_float $out_sum_float MB"
echo
}
# Main loop
while true; do
if [ "$paused" = false ]; then
if [ "$err" = false ]; then
calculate_data_rate #comment with difficulties with (hard)links, permissions, 'No such file or directory',
fi
monitor_io
fi
read -s -t 1 -n 1 key
if [ "$key" = "q" ] || [ "$key" = "Q" ]; then
echo "monitoring stop: $(date)"
echo
sleep 1
break
exit 1
elif [ "$key" = "p" ]; then
paused=true
echo "Output paused. Press space or key 'r' to resume."
elif [ "$key" = " " ] || [ "$key" = "r" ]; then
paused=false
echo "Output resumed."
elif [ "$key" = "n" ]; then
if [ "$err" == "false" ]; then
err="true"
else
err="false"
fi
elif [ ! "$key" == "" ]; then
echo ""
echo "key(s) pressed: $key"
echo ""
fi
done