]> 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:

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