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

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