stat.awk 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/awk -f
  2. BEGIN {
  3. scriptname = ENVIRON["SCRIPT_NAME"]
  4. #for win32
  5. isql=".\\isqlodbc.exe sqlite "
  6. #for unix
  7. #isql="./isqlodbc sqlite "
  8. print "Content-Type: text/html; charset=koi8-r \n\n"
  9. print "<HTML>\n<BODY>\n";
  10. # query parse
  11. query_str = ENVIRON["QUERY_STRING"]
  12. n = split(query_str, querys, "&")
  13. for (i=1; i<=n; i++)
  14. {
  15. split(querys[i], data, "=")
  16. qr[data[1]] = data[2]
  17. }
  18. printf "<FORM METHOD=PUT action=\"" scriptname "?rep=1\">"
  19. printf "datefrom:<INPUT name=\"datefrom\" value=\"2004-06-01\"> "
  20. printf "dateto:<INPUT name=\"dateto\" value=\"2004-07-30\"> <br>"
  21. printf "<INPUT type=\"radio\" name=\"userid\" value=\"username\" checked> LOGIN user <br>"
  22. printf "<INPUT type=\"radio\" name=\"userid\" value=\"userip\"> IP user <br>"
  23. printf "<INPUT type=\"hidden\" name=\"rep\" value=\"user\">"
  24. printf "<INPUT type=\"submit\" value=\"Report\">"
  25. printf "</FORM>"
  26. #printf "query_str=%s\n<br>",query_str
  27. #print qr["rep"]
  28. if(qr["rep"]=="user")
  29. {
  30. cmd = isql " \"select " qr["userid"] ",sum(bytein),sum(byteout),sum(bytein+byteout) from log \
  31. where ldate > '" qr["datefrom"] "' AND ldate < '" qr["dateto"] \
  32. "' group by " qr["userid"] " order by sum(bytein+byteout) desc;\""
  33. printf " <table WIDTH=100%% BORDER=1><tr><td><b>user</b></td> <td><b>bytein</b></td> <td><b>byteout</b> </td> <td> <b>bytesum</b></td></tr>"
  34. while( (cmd|getline result)>0)
  35. {
  36. split(result, rt, "|")
  37. printf "<tr> <td><a href=\"%s?rep=host&datefrom=%s&dateto=%s&userid=%s&selectid=%s\"> %s <\/a></td><td>%d</td><td>%d</td><td>%d</td></tr>",
  38. scriptname,qr["datefrom"],qr["dateto"],qr["userid"],rt[1],rt[1],rt[2],rt[3],rt[4]
  39. totalbytein=totalbytein+rt[2];
  40. totalbyteout=totalbyteout+rt[3];
  41. totalbytesum=totalbytesum+rt[4];
  42. }
  43. printf "<tr> <td><br>Total users</td> <td><br>%d</td> <td><br>%d</td> \
  44. <td><br>%d</td></tr> </table> ",totalbytein,totalbyteout, totalbytesum
  45. close(cmd)
  46. }
  47. if(qr["rep"]=="host")
  48. {
  49. cmd = isql "\"select sum(bytein+byteout), sum(bytein), sum(byteout),host from log \
  50. where ldate > '" qr["datefrom"] "' AND ldate < '"qr["dateto"] \
  51. "' AND " qr["userid"] " = '" qr["selectid"] \
  52. "' group by host order by sum(bytein+byteout) desc;\""
  53. printf "<center><b>Detail statistic for user: %s</b></center>",qr["selectid"]
  54. printf " <table WIDTH=100%% BORDER=1> <tr><td><b>sum byte</b></td> <td><b>bytein</b></td> <td><b>byteout</b></td><td><b>host</b></td></tr>"
  55. while( (cmd|getline result)>0)
  56. {
  57. split(result, rt, "|")
  58. printf "<tr><td>%d</td><td>%d</td><td>%d</td><td>%s</td></tr>",rt[1],rt[2],rt[3],rt[4]
  59. totalbytein=totalbytein+rt[1];
  60. totalbyteout=totalbyteout+rt[2];
  61. totalbytesum=totalbytesum+rt[3];
  62. }
  63. printf "<tr> <td><br>%d</td> <td><br>%d</td> \
  64. <td><br>%d</td><td><br>Total host</td></tr> </table> ",totalbytein,totalbyteout, totalbytesum
  65. printf " </table> "
  66. close(cmd)
  67. }
  68. printf " </BODY> </HTML>";
  69. } # end BEGIN
  70. # decode urlencoded string
  71. function decode(text, hex, i, hextab, decoded, len, c, c1, c2, code) {
  72. split("0 1 2 3 4 5 6 7 8 9 a b c d e f", hex, " ")
  73. for (i=0; i<16; i++) hextab[hex[i+1]] = i
  74. # urldecode function from Heiner Steven
  75. # http://www.shelldorado.com/scripts/cmds/urldecode
  76. # decode %xx to ASCII char
  77. decoded = ""
  78. i = 1
  79. len = length(text)
  80. while ( i <= len ) {
  81. c = substr (text, i, 1)
  82. if ( c == "%" )
  83. {
  84. if ( i+2 <= len )
  85. {
  86. c1 = tolower(substr(text, i+1, 1))
  87. c2 = tolower(substr(text, i+2, 1))
  88. if ( hextab [c1] != "" || hextab [c2] != "" ) {
  89. if ( (c1 >= 2 && (c1 != 7 && c2 != "F")) || (c1 == 0 && c2 ~ "[9acd]") )
  90. {
  91. code = 0 + hextab [c1] * 16 + hextab [c2] + 0
  92. c = sprintf ("%c", code)
  93. }
  94. else { c = " " }
  95. i = i + 2
  96. }
  97. }
  98. } else if ( c == "+" ) { # special handling: "+" means " "
  99. c = " "
  100. }
  101. decoded = decoded c
  102. ++i
  103. }
  104. # change linebreaks to \n
  105. gsub(/\r\n/, "\n", decoded)
  106. # remove last linebreak
  107. sub(/[\n\r]*$/,"",decoded)
  108. return decoded
  109. }