Advertisement
Guest User

printlog.cmd

a guest
Oct 4th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 6.74 KB | None | 0 0
  1. @Echo Off
  2. SetLocal EnableDelayedExpansion
  3. :: Инициализация и проверки
  4. Set StartedAt=%Time%
  5. Set Data=%*
  6. :Next
  7. For /F "tokens=1,* delims=;" %%A In ("%Data%") Do (
  8.     For /F "tokens=1,2 delims==" %%C In ("%%A") Do (Set $%%C=%%D)
  9.     If Not "%%B"=="" Set Data=%%B&GoTo :Next
  10. )
  11. If Not Defined H-Cookie Set "H-Cookie=dummy"
  12. Set H-Cookie|Findstr /C:"<" /C:">" /C:"&" /C:"|" /C:^"\^"^" /C:"(" /C:")" /C:"\^">nul&&(Call HTTP302Header err_badcookie&Exit)
  13. For %%A In ("%H-Cookie:;=" "%") Do For /F "tokens=1,* delims== " %%A In ("%%~A") Do Set "C-%%A=%%B"
  14. If Not Defined C-Code Set "LoginStatus=err_nosession" & GoTo :Back
  15. For %%A In (User Access LastSeen) Do Set "%%A="
  16. For /F "tokens=1-3 delims=|" %%A In ('Call GetUserInfo %H-REMOTE_ADDR% %C-Code%') Do (
  17.     Set "User=%%A"
  18.     Set "Access=%%B"
  19.     Set "LastSeen=%%C"
  20. )
  21. If Not Defined Access Set "LoginStatus=err_nosession" & GoTo :Back
  22. If %Access% LSS 5 Set "LoginStatus=err_noaccess" & GoTo :Back
  23.  
  24. Set RDPUser=Username
  25. Set RDPPassword=Password
  26. Set PrintServerIP=192.168.0.201
  27. Set DB=%__ServerRoot%\data\printlog.db
  28. Set Table=printlog-%date:~3%
  29. Set Logs=%__ServerRoot%\data\printlogs
  30. Set Names[]="Вася Пупкин","Антон Безымянный","Иван Иванов","Васисуалий Шишкин","John Doe"
  31. rem NetBIOS, IPv4 или IPv6
  32. Set Fingerprints[]="PC-1=Вася Пупкин",^
  33.     "192.168.0.201=Вася Пупкин",^
  34.     "PC-2=Антон Безымянный",^
  35.     "[fe80::b42b:9127:da45:f59a]=Антон Безымянный",^
  36.     "192.168.0.202=Антон Безымянный",^
  37.     "PC-3=Иван Иванов",^
  38.     "[fe80::54c9:58d0:96c0:d848]=Иван Иванов",^
  39.     "[fe80::d82c:38a3:8667:9eb]=Иван Иванов",^
  40.     "192.168.0.203=Иван Иванов",^
  41.     "PC-4=Васисуалий Шишкин",^
  42.     "[fe80::3c4c:209a:9b62:56ad]=Васисуалий Шишкин",^
  43.     "192.168.0.204=Васисуалий Шишкин",^
  44.     "DIRECTOR=John Doe",^
  45.     "[fe80::d92f:f9a6:10b9:fcbd]=John Doe",^
  46.     "192.168.0.200=John Doe"
  47.  
  48.  
  49. rem Клиентский скрипт копирует логи через $D
  50. psexec -accepteula -n 3 -u %RDPUser% -p %RDPPassword% -s \\%PrintServerIP% cmd /c printlog >nul 2>nul
  51.  
  52. Call :CheckDatabase
  53. Call :GetDatabaseTables
  54. Call :GetLastTimestamp
  55. If Defined $page If Defined table[%$page%] (
  56.     Set Table=!table[%$page%]!
  57.     Set active_table=%$page%
  58. )
  59. For /F "tokens=2 delims=-" %%A In ("%Table%") Do Set LogVisibleDate=%%A
  60.  
  61. :AddLogsToDB
  62. For /F "skip=2 tokens=1,3-4,6-7 delims=," %%A In ('type "%Logs%\monthly.log"^|sort /R') Do (
  63.     For /F "delims=" %%T In ('Call TimeToUnix %%A') Do Set timestamp=%%T
  64.     If  !timestamp! LEQ !Last! GoTo :break
  65.     rem Set "author=%%E"
  66.     Set docname=%%~D
  67.     Set docname=!docname:^|=!
  68.     Set docname=!docname:^<=!
  69.     Set docname=!docname:^>=!
  70.     Set docname=!docname:^&=!
  71.     For /F "delims=" %%N In ('Echo !docname!^|recode cp1251..utf-8') Do Set document=%%N
  72.     For %%X In (%Fingerprints[]%) Do (
  73.         For /F "tokens=1,* delims==" %%M In ("%%~X") Do (
  74.             If "%%E" == "%%~M" Set "author=%%~N"
  75.         )
  76.     )
  77.     Echo INSERT INTO `%Table%` ^(timestamp, time, pages, copies, document, author^) VALUES ^(!timestamp!, "%%A", %%B, %%C, "!document!", "!author!"^);|sqlite3 "%DB%"
  78. )
  79. :break
  80.  
  81. :Stats
  82. Set Users=0
  83. Set Total.pages=0
  84. For %%N In (%Names[]%) Do (
  85.     Set /A Users+=1
  86.     Set User_!Users!.name=%%~N
  87.     Set User_!Users!.documents=0
  88.     Set User_!Users!.pages=0
  89.     For /F "tokens=1-2 delims=|" %%A In ('Echo SELECT `pages`^, `copies` FROM `%Table%` WHERE `author` like "%%~N"^;^|sqlite3 "%DB%"') Do (
  90.         Set /A User_!Users!.documents+=1
  91.         Set /A User_!Users!.pages+=%%A*%%B
  92.         Set /A Total.pages+=%%A*%%B
  93.     )
  94. )
  95.  
  96. For /F "delims=" %%A In ('GetLocalPath "%~dp0"') Do Set "LocalPath=%%A"
  97. rem Call RecompileTemplates
  98. Call HTTP200Header
  99. Call Include HTMLHeader "Title:Статистика печати за %LogVisibleDate%"
  100. Call Include Menu "cpanel:true;login:true;Username:%User%;Self:%LocalPath%;Code:%C-Code%"
  101. Call :MakeJSON
  102. Call Include-rawJSON PrintLog "%JSONFile%"
  103. For /F "delims=" %%A In ('LoadTimer %StartedAt%') Do (Set ExecTime=%%A)
  104. Call Include Footer "PerformanceData:%ExecTime%;ExecutableName:%~nx0;ServerVersion:%__OWS_version%"
  105. Call Include HTMLEnd
  106. Exit
  107.  
  108. :MakeJSON
  109. Set JSONFile=%Temp%\printlog_%random%%C-Code%.json
  110. <Nul Set /P Echo="{"Users":[">"%JSONFile%"
  111. For /L %%X In (1,1,!Users!) Do (
  112.     <Nul Set /P Echo="{"
  113.     <Nul Set /P Echo=""ID":"%%X","Name":"!User_%%X.name!","Docs":"!User_%%X.documents!","Pages":"!User_%%X.pages!""
  114.     If "!Users!"=="%%X" (<Nul Set /P Echo="}") Else (<Nul Set /P Echo="},")
  115. )>>"%JSONFile%"
  116. <Nul Set /P Echo="],"TotalPages":"!Total.pages!",">>"%JSONFile%"
  117.  
  118. Set i=0
  119. Set Docs=0
  120. For /F "delims=" %%X In ('Echo SELECT `id` FROM `%Table%`^;^|sqlite3 "%DB%"') Do (Set /A Docs+=1)
  121. <Nul Set /P Echo=""Documents":[">>"%JSONFile%"
  122. For /F "tokens=1-5 delims=|" %%A In ('Echo SELECT `time`^, `author`^, `pages`^, `copies`^, `document` FROM `%Table%` ORDER BY `timestamp` DESC^;^|sqlite3 "%DB%"') Do (
  123.     Set /A i+=1
  124.     <Nul Set /P Echo="{"
  125.     <Nul Set /P Echo=""Timestamp":"%%A","User":"%%B","Pages":"%%C","Copies":"%%D","Filename":"%%E""
  126.     If "!Docs!"=="!i!" (<Nul Set /P Echo="}") Else (<Nul Set /P Echo="},")
  127. )>>"%JSONFile%"
  128. <Nul Set /P Echo="],"LogPages":"%table[index]%","ActiveLogPage":"%active_table%","LogVisibleDate":"%LogVisibleDate%","Session":"%C-Code%"}">>"%JSONFile%"
  129. Exit /B
  130.  
  131.  
  132.  
  133. :CheckDatabase
  134. :: Проверяем наличие БД и создаём при надобности
  135. Echo CREATE TABLE IF NOT EXISTS `%Table%` (^
  136.     `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,^
  137.     `timestamp` BIGINT,^
  138.     `time` TEXT,^
  139.     `pages` INT,^
  140.     `copies` INT,^
  141.     `document` TEXT,^
  142.     `author` TEXT^
  143. );|sqlite3 "%DB%"
  144. Exit /B
  145.  
  146. :GetDatabaseTables
  147. Set table[index]=0
  148. Set active_table=1
  149. For /F "delims= tokens=*" %%A In ('Echo SELECT name FROM sqlite_master WHERE name LIKE "printlog-%%" AND  type ^= "table" ORDER BY name ASC^;^|sqlite3 "%DB%"') Do (
  150.     Set /A table[index]+=1
  151.     Set table[!table[index]!]=%%A
  152.     If "%%A" == "%Table%" Set active_table=!table[index]!
  153. )
  154. Exit /B
  155.  
  156. :GetLastTimestamp
  157. Set "Last="
  158. For /F "delims=" %%A In ('Echo SELECT `timestamp` FROM `%Table%` ORDER BY `timestamp` DESC LIMIT 1^;^|sqlite3 "%DB%"') Do (Set Last=%%A)
  159. If Not Defined Last Set Last=1400000000
  160. Exit /B
  161.  
  162. :Back
  163. :: Логгируем ошибки и редиректим
  164. If "%LoginStatus%"=="err_noaccess" Echo [%Date% %Time:~,8%] [IP:%H-REMOTE_ADDR%] tried to execute "%~nx0" with access level "%Access%">>"%__ServerRoot%\%__AccessLogFile%"
  165. If "%LoginStatus%"=="err_nosession" Echo [%Date% %Time:~,8%] Can't validate session token [Code:"%C-Code%"] for access [IP:%H-REMOTE_ADDR%] to "%~nx0">>"%__ServerRoot%\%__AccessLogFile%"
  166. If "%LoginStatus%"=="err_nosession" Call HTTP302Header-nosession
  167. If "%LoginStatus%"=="err_noaccess" Call HTTP302Header err_noaccess
  168. Exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement