]> git.madduck.net Git - etc/awesome.git/blob - scripts/dfs

madduck's git repository

Every one of the projects in this repository is available at the canonical URL git://git.madduck.net/madduck/pub/<projectpath> — see each project's metadata for the exact URL.

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.madduck.net. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

SSH access, as well as push access can be individually arranged.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.madduck.net/madduck/"]
  insteadOf = madduck:

1de819a94ba2813867b8fb2d19197fafc8e0a871
[etc/awesome.git] / scripts / dfs
1 #!/bin/bash
2 #
3 #   Adapted from Eridan's "fs" (cleanup, enhancements and switch to bash/Linux)
4 #   JM,  10/12/2004
5 #
6 #   Integrated into Lain in september 2013
7 #   https://github.com/copycat-killer/lain
8
9 #   Requires gawk
10
11 # -------------------------------------------------------------------------
12 #   Decoding options
13 # -------------------------------------------------------------------------
14 USAGE="Usage: $0 [-h(elp)] | [-n(arrow mode)] | [-w(eb output) | --type=<fstype> | --exclude-type=<fstype>]"
15
16 NARROW_MODE=0
17 WEB_OUTPUT=0
18 DF_OPTIONS=""
19
20 while [ $# -gt 0 ]; do
21 case "$1" in
22 "-h" )
23 echo $USAGE
24 exit
25 ;;
26 "-d" )
27 DEBUG=1
28 ;;
29 "-n" )
30 NARROW_MODE=1
31 ;;
32 "-w" )
33 WEB_OUTPUT=1
34 ;;
35 --type=*)
36 DF_OPTIONS+=" $1"
37 ;;
38 --exclude-type=*)
39 DF_OPTIONS+=" $1"
40 ;;
41 * )
42 echo $USAGE
43 exit
44 ;;
45 esac
46 shift
47 done
48
49 # -------------------------------------------------------------------------
50 #   Preparations
51 # -------------------------------------------------------------------------
52 SYSTEM=`uname -s`
53 PATTERN="/"
54
55 case "$SYSTEM" in
56 "Linux" )
57 DF_COMMAND="/usr/bin/env df -k"
58 SORT_COMMAND="/usr/bin/env sort -k6"
59 AWK_COMMAND="/usr/bin/env awk"
60 ;;
61 * )
62 DF_COMMAND="/bin/df -k"
63 SORT_COMMAND="/usr/bin/sort -k6"
64 AWK_COMMAND="/usr/bin/env gawk"
65 ;;
66 esac
67
68 # Add additional df options
69 DF_COMMAND+=$DF_OPTIONS
70
71 # -------------------------------------------------------------------------
72 #   Grabbing "df" result
73 # -------------------------------------------------------------------------
74 DF_RESULT=`$DF_COMMAND`
75 if [ ! -z $DEBUG ]; then
76 echo "--> DF_RESULT:"
77 echo "$DF_RESULT"
78 echo ""
79 fi
80
81 # -------------------------------------------------------------------------
82 #   Preprocessing "df" result, to join split logical lines
83 # -------------------------------------------------------------------------
84 PREPROCESSING_RESULT=` \
85                                                                                  echo "$DF_RESULT" | $AWK_COMMAND -v PATTERN=$PATTERN \
86                                                                                  '
87                                                                                  NF == 1 {
88                                                                                          printf ("%s", $0)
89                                                                                  }
90
91 NF == 5 {
92         printf ("%s\n", $0)
93 }
94
95 NF > 6  {
96 }
97
98 NF == 6 {
99         printf ("%s\n", $0)
100 }'
101 `
102 if [ ! -z $DEBUG ]; then
103 echo "--> PREPROCESSING_RESULT:"
104 echo "$PREPROCESSING_RESULT"
105 echo ""
106 fi
107
108 SORTED_FILE_SYSTEMS_INFO=`echo "$PREPROCESSING_RESULT" | $SORT_COMMAND`
109
110 if [ ! -z $DEBUG ]; then
111 echo "--> SORTED_FILE_SYSTEMS_INFO:"
112 echo "$SORTED_FILE_SYSTEMS_INFO"
113 echo ""
114 fi
115
116 # -------------------------------------------------------------------------
117 #   Computing mount point max length
118 # -------------------------------------------------------------------------
119 MOUNT_POINT_MAX_LENGTH=` \
120                                                                                          echo "$SORTED_FILE_SYSTEMS_INFO" | $AWK_COMMAND -v PATTERN=$PATTERN \
121                                                                                          '
122                                                                                          BEGIN       {
123                                                                                                  mount_point_length_max = 15;
124                                                                                          }
125
126 END     {
127         printf ("%d", mount_point_length_max);
128 }
129
130 $0 ~ PATTERN    {
131 #       printf ("$6 = %s\n", $6);
132
133         mount_point = $6;
134 #       printf ("mount_point = %s\n", mount_point);
135
136         mount_point_length = length (mount_point);
137 #       printf ("mount_point_length = %d\n", mount_point_length);
138
139         if (mount_point_length > mount_point_length_max)
140                 mount_point_length_max = mount_point_length;
141 }'
142 `
143 if [ ! -z $DEBUG ]; then
144 echo "MOUNT_POINT_MAX_LENGTH:      $MOUNT_POINT_MAX_LENGTH"
145 fi
146
147 # -------------------------------------------------------------------------
148 #   Computing mount point data max size
149 # -------------------------------------------------------------------------
150 MOUNT_POINT_MAX_SIZE=` \
151                                                                                  echo "$SORTED_FILE_SYSTEMS_INFO" | $AWK_COMMAND -v PATTERN=$PATTERN \
152                                                                                  '
153                                                                                  BEGIN       {
154                                                                                          mount_point_size_max = 0;
155                                                                                  }
156
157 END     {
158         printf ("%d", mount_point_size_max);
159 }
160
161 $0 ~ PATTERN    {
162 #       df -k shows k_bytes!
163 #       printf ("$2 = %s\n", $2);
164
165         mount_point_size = $2 * 1024;
166 #       printf ("mount_point_size = %d\n", mount_point_size);
167
168         if (mount_point_size > mount_point_size_max)
169                 mount_point_size_max = mount_point_size;
170 }'
171 `
172 if [ ! -z $DEBUG ]; then
173 echo "MOUNT_POINT_MAX_SIZE:      $MOUNT_POINT_MAX_SIZE"
174 fi
175
176 # -------------------------------------------------------------------------
177 #   Let's go!
178 # -------------------------------------------------------------------------
179 echo "$SORTED_FILE_SYSTEMS_INFO" | $AWK_COMMAND -v DEBUG=$DEBUG -v PATTERN=$PATTERN -v NARROW_MODE=$NARROW_MODE -v LEFT_COLUMN=$MOUNT_POINT_MAX_LENGTH -v MAX_SIZE=$MOUNT_POINT_MAX_SIZE -v SCALE=$SCALE -v WEB_OUTPUT=$WEB_OUTPUT \
180                          '
181 #   {printf ("$0 = %s\n", $0);}
182 #   {printf ("$1 = %s\n", $1);}
183 #   {printf ("PATTERN = %s\n", PATTERN);}
184 #   {printf ("LEFT_COLUMN = %s\n", LEFT_COLUMN);}
185
186                          BEGIN       {
187                                  k_bytes = 1024.0;
188                                  m_bytes = 1024.0 * k_bytes;
189                                  g_bytes = 1024.0 * m_bytes;
190                                  t_bytes = 1024.0 * g_bytes;
191
192                                  if (WEB_OUTPUT)
193                                  {
194                                          all_stars = "**************************************************";
195                                          current_date = strftime ("%d-%m-%Y @ %H:%M:%S", localtime (systime ()));
196                                          free_threshold = 10; # %
197
198                                  printf ("<!-- DEBUT CONTENU -->\n");
199
200                                          printf ( \
201                                                          "<A NAME=\"top\"></A>\n" \
202                                                          "<P ALIGN=CENTER><SPAN CLASS=\"titleblue\">%s</SPAN><SPAN CLASS=\"textbold\">  --  STATUS OF <SPAN CLASS=\"titlered\">ALCOR</SPAN> FILE SYSTEMS</SPAN></P><BR>\n",
203                                                          current_date )
204
205                                                  printf ("<TABLE WIDTH=\"100%%\" BORDER=1>\n");
206
207                                          printf ( \
208                                                          "<TR>\n" \
209                                                          "<TD ALIGN=LEFT><STRONG>Mount point</STRONG></TD>\n" \
210                                                          "<TD ALIGN=CENTER><STRONG>%% Usato&nbsp;(<SPAN CLASS=\"titleblue\">*</SPAN>)" \
211                                                          "&nbsp;-&nbsp;%% Free&nbsp;(<SPAN CLASS=\"titlegreen\">*</SPAN>)</STRONG></TD>\n" \
212                                                          "<TD ALIGN=CENTER><STRONG>%% Used</STRONG></TD>\n" \
213                                                          "<TD ALIGN=CENTER><STRONG>Free</STRONG></TD>\n" \
214                                                          "<TD ALIGN=CENTER><STRONG>Total</STRONG></TD>\n" \
215                                                          "</TR>\n" );
216                                  }
217                                  else
218                                  {
219                                          narrow_margin = "       ";
220 #           printf ("%-*s", LEFT_COLUMN + 2, "Mount point");
221                                                  if (NARROW_MODE)
222                                                          printf ("\n%s", narrow_margin);
223                                                  else
224                                                          printf ("%-*s", LEFT_COLUMN + 2, "");
225                                          print "                                                    Used     Free       Total ";
226                                          if (! NARROW_MODE)
227                                                  print "";
228                                  }
229                          }
230
231 END     {
232         if (WEB_OUTPUT)
233         {
234                 printf ("</TABLE>\n");
235
236                 printf ("<!-- FIN CONTENU -->\n");
237         }
238         else
239         {
240                 if (NARROW_MODE)
241                         printf ("%s", narrow_margin);
242                 else
243                         printf ("%-*s", LEFT_COLUMN + 2, "");
244                 print "|----|----|----|----|----|----|----|----|----|----|"
245                         if (NARROW_MODE)
246                                 printf ("\n%s", narrow_margin);
247                         else
248                                 printf ("%-*s", LEFT_COLUMN + 2, "");
249                 print "0   10   20   30   40   50   60   70   80   90  100";
250                 print "";
251         }
252 }
253
254 $0 ~ PATTERN    {
255
256         if (index ($0, "members") == 0 && index ($0, "Download") == 0 && index ($0, "admin") == 0)
257         {
258 #       df -k shows k_bytes!
259
260                 total_size = $2 * k_bytes;
261                 free_size = $4 * k_bytes;
262                 percentage_occupied = substr($5, 0, 3);
263                 mount_point = $6;
264
265                 percentage_free = int (100 - percentage_occupied);
266
267 #       reduction_factor: 2
268                 stars_number = int (percentage_occupied / 2);
269
270                 if (WEB_OUTPUT)
271                 {
272                         posGroup = index (mount_point, "scratch");
273                         if (posGroup == 0)
274                                 posGroup = index (mount_point, "u1");
275                         if (posGroup == 0)
276                                 posGroup = index (mount_point, "u2");
277                         if (posGroup == 0)
278                                 posGroup = index (mount_point, "u4");
279                         if (posGroup == 0)
280                                 posGroup = index (mount_point, "u5");
281
282                         printf ("<TR>\n");
283
284                         if (posGroup > 0 || percentage_free < free_threshold)
285                         {
286                                 if (percentage_free < free_threshold)
287                                 {
288                                         class = "titlered";
289                                         if (posGroup == 0)
290                                                 posGroup = 1;   # to display the whole mount_point in this color anyway
291                                 }
292                                 else if ((index (mount_point, "scratch") != 0) || (index (mount_point, "u1") != 0) || (index (mount_point, "u2") != 0))
293                                 {
294                                         class = "titleorange";
295                                         posGroup = 1;   # to display the whole mount_point in this color
296                                 }
297                                 else if ((index (mount_point, "u4") != 0) || (index (mount_point, "u5") != 0))
298                                 {
299                                         class = "titlebrown";
300                                         posGroup = 1;   # to display the whole mount_point in this color
301                                 }
302
303                                 printf ( \
304                                                 "<TD ALIGN=LEFT>%s<SPAN CLASS=\"%s\">%s</SPAN></TD>\n",
305                                                 substr (mount_point, 1, posGroup - 1),
306                                                 class,
307                                                 substr (mount_point, posGroup) );
308                         }
309                         else
310                         {
311                                 printf ("<TD ALIGN=LEFT>%s</TD>\n", mount_point);
312                         }
313
314                         printf ( \
315                                         "<TD ALIGN=CENTER><SPAN CLASS=\"titleblue\">%s</SPAN><SPAN CLASS=\"titlegreen\">%s</SPAN></TD>\n",
316                                         substr (all_stars, 1, stars_number), substr (all_stars, stars_number + 1, 49) );
317
318                         if (percentage_free < free_threshold)
319                         {
320                                 color_beginning = "<SPAN CLASS=\"titlered\">";
321                                 color_end = "</SPAN>"
322                         }
323                         else
324                         {
325                                 color_beginning = "";
326                                 color_end = ""
327                         }
328
329                         if (total_size > 1 * t_bytes)
330                                 printf ( \
331                                                 "<TD ALIGN=RIGHT>%s%3d%%%s</TD><TD ALIGN=RIGHT>%5.1f Tb</TD><TD ALIGN=RIGHT>%5.1f Tb</TD>\n", \
332                                                 color_beginning, percentage_occupied, color_end, free_size / t_bytes, total_size / t_bytes \
333                                                 );
334                         else if (total_size > 1 * g_bytes)
335                                 printf ( \
336                                                 "<TD ALIGN=RIGHT>%s%3d%%%s</TD><TD ALIGN=RIGHT>%5.1f Gb</TD><TD ALIGN=RIGHT>%5.1f Gb</TD>\n", \
337                                                 color_beginning, percentage_occupied, color_end, free_size / g_bytes, total_size / g_bytes \
338                                                 );
339                         else if (total_size > 1 * m_byptes)
340                                 printf ( \
341                                                 "<TD ALIGN=RIGHT>%s%3d%%%s</TD><TD ALIGN=RIGHT>%5.1f Mb</TD><TD ALIGN=RIGHT>%5.1f Mb</TD>\n", \
342                                                 color_beginning, percentage_occupied, color_end, free_size / m_bytes, total_size / m_bytes \
343                                                 );
344                         else
345                                 printf ( \
346                                                 "<TD ALIGN=RIGHT>%s%3d%%%s</TD><TD ALIGN=RIGHT>%5.1f Kb</TD><TD ALIGN=RIGHT>%5.1f Kb</TD>\n", \
347                                                 color_beginning, percentage_occupied, color_end, free_size / k_bytes, total_size / k_bytes \
348                                                 );
349
350                         printf ("</TR>\n");
351                 }
352
353                 else
354                 {
355 #           printf ("percentage_occupied = %d\n", percentage_occupied);
356 #           printf ("percentage_free = %d\n", percentage_free);
357
358                         printf ("%-*s", LEFT_COLUMN + 2, mount_point);
359                         if (NARROW_MODE)
360                                 printf ("\n%s", narrow_margin);
361
362 #           printf ("stars_number = %d\n", stars_number);
363
364                         printf ("|");
365                         for (i = 1; i <= stars_number && i <= 49; i++)
366                         {
367                                 printf ("%s", "*");
368                         }
369                         for (i = stars_number + 1; i <= 49; i++)
370                         {
371                                 printf ("%s", "-");
372                         }
373
374
375                         if (total_size > 1 * t_bytes)
376                                 printf ( \
377                                                 "| %3d%%   %6.1f   %6.1f Tb\n", \
378                                                 percentage_occupied, free_size / t_bytes, total_size / t_bytes \
379                                                 );
380                         else if (total_size > 1 * g_bytes)
381                                 printf ( \
382                                                 "| %3d%%   %6.1f   %6.1f Gb\n", \
383                                                 percentage_occupied, free_size / g_bytes, total_size / g_bytes \
384                                                 );
385                         else if (total_size > 1 * m_byptes)
386                                 printf ( \
387                                                 "| %3d%%   %6.1f   %6.1f Mb\n", \
388                                                 percentage_occupied, free_size / m_bytes, total_size / m_bytes \
389                                                 );
390                         else
391                                 printf ( \
392                                                 "| %3d%%   %6.1f   %6.1f Kb\n", \
393                                                 percentage_occupied, free_size / k_bytes, total_size / k_bytes \
394                                                 );
395                 }
396         }   # if
397 }'