Code : Tout sélectionner
#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <SQLite.au3>
#include <SQLite.Dll.au3>
#include <AD.au3>
#include <Array.au3>
#include <Process.au3>
Global $file1 = @ScriptDir & '\log_spooler.txt'
Global $file = @ScriptDir & "\log_spooler_" & @MON & ".txt"
;~ Global $nomfile = "papercut-print-log-" & @YEAR & "-" & @MON & ".csv"
;~ Global $Patchfile = @ScriptDir
Global Const $impBE[5] = [ _
"LEXMARK-E352DN-RDC-01", _
"HP-P2055DN-RDC", _
"LEXMARK-E360DN-1E", _
"OCE-3155-1E", _
"LEXMARK-X363DN-1E-01" _
]
Global Const $impBCR[3] = [ _
"LEXMARK-E352DN-RDC-03", _
"HP-CP3525DN-RDC", _
"LEXMARK-E352DN-RDC-04" _
]
Global Const $impBRNE[2] = [ _
"OCE-3155-2EXT", _
"LEXMARK-E360DN-2EXT" _
]
FileCopy($file1, $file, 9)
_SQLite_Startup()
OnAutoItExitRegister("_SQLite_ShutDown")
Global $hDb = _SQLite_Open(@ScriptDir & "\SpoolLog_" & @MON & ".db3")
; si OK ...
_SQLite_Exec($hDb, "CREATE TABLE if not exists Log (" & _
" Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," & _
" Date CHAR," & _
" User CHAR," & _
" Pages INTEGER," & _
" Copies INTEGER," & _
" Printer CHAR," & _
" Document CHAR," & _
" Client CHAR," & _
" Paper CHAR," & _
" Language CHAR," & _
" Height INTEGER," & _
" Width INTEGER," & _
" Duplex BOOLEAN," & _
" Grayscale BOOLEAN," & _
" SizeKb INTEGER," & _
" CONSTRAINT ksUniq UNIQUE(Date, User, Printer, Document) ON CONFLICT REPLACE);" & _
"CREATE INDEX if not exists ixDate ON Log (Date);" & _
"CREATE INDEX if not exists ixUser ON Log (User);" & _
"CREATE INDEX if not exists ixPrinter ON Log (Printer);")
Global $aLines, $res
_FileReadToArray($file, $aLines)
_SQLite_SetTimeout($hDb, 60000) ; on laisse jusqu'à 60 s de timeout, juste au cas où la base serait exploitée/exploitable par un autre process
_SQLite_Exec($hDb, "begin immediate;") ; on lance une transaction avant l'insertion en boucle, pour la performance
For $line In $aLines
$res = StringRegExp($line, '([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),"([^,]*)",([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*),([^,]*)kb,?', 1)
If Not @error Then
$res[11] = Number($res[1] = 'DUPLEX')
$res[12] = Number($res[12] = 'GRAYSCALE')
_SQLite_Exec($hDb, "insert into log ( date, user, pages, copies, printer, " & _
"document, client, paper, language, height, " & _
" width, duplex, grayscale, sizeKb) " & _
"values (" & X($res[0]) & XX($res[1]) & ZZ($res[2]) & ZZ($res[3]) & XX($res[4]) & _
XX($res[5]) & XX($res[6]) & XX($res[7]) & XX($res[8]) & ZZ($res[9]) & _
ZZ($res[10]) & ZZ($res[11]) & ZZ($res[12]) & ZZ($res[13]) & ");")
EndIf
Next
_SQLite_Exec($hDb, "commit;")
; Open Connection to the Active Directory
_AD_Open()
; Close Connection to the Active Directory on Exit
OnAutoItExitRegister("_AD_Close")
BE()
_SQLite_Close($hDb)
Func BE()
Local $rows, $nrows
For $printer In $impBE
ReqBD_BE($printer, $rows, $nrows)
ResBE($rows, $nrows)
Next
EndFunc ;==>BE
Func ResBE(ByRef $rows, ByRef $nrows)
Local $user, $aUser, $pageA4, $pageA3, $filegroup, $rFile, $filecount
For $i = 0 To $nrows - 1
;~ MsgBox(0,"user","user "&$rows[$i][0] & " nbs pages "&$rows[$i][2])
$user = $rows[$i][0]
$pageA4 = $rows[$i][2]
$pageA3 = $rows[$i][1]
;~ MsgBox(0,"user","user "&$rows[$i][0])
;~ MsgBox(0,"user","nbs pages "&$page)
$aUser = _AD_GetUserGroups($user)
If @error > 0 Then
MsgBox(64, "Active Directory Functions - Example 1", "User '" & $user & "' has not been assigned to any group")
Else
; For groups that are inherited, the return is the FQDN of the group or user, and the FQDN(s) of the group(s) it
; was inherited from, seperated by '|'
;~ _ArrayDisplay($aUser, "Active Directory Functions - Example 1 - Group names user '" & $user & "' is a member of")
For $j = 1 To $aUser[0]
;~ MsgBox(0,"group user","user member of group "&$aUser[$j])
$filegroup = FileOpen("groupBE.txt", 1)
If $filegroup = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
FileWriteLine($filegroup, $aUser[$j] & @CRLF)
FileClose($filegroup)
Next
EndIf
$filegroup = FileOpen("groupBE.txt", 0)
While 1
If $filegroup = -1 Then
MsgBox(0, "Error", "Unable to open file.")
Exit
EndIf
$rFile = FileReadLine($filegroup)
If @error = -1 Then ExitLoop
$cn = StringSplit($rFile, ",")
;~ MsgBox(0, "group", $rFile)
;~ MsgBox(0, "group cn=", $cn[1])
If $cn[1] = "CN=xxxxx" Then
$filecount = FileOpen("countBE.txt", 1)
FileWriteLine($filecount, $pageA4)
FileWriteLine($filecount, $pageA3)
FileClose($filecount)
ExitLoop
EndIf
WEnd
FileClose($filegroup)
Next
EndFunc ;==>ResBE
Func ReqBD_BE($imp, ByRef $rows, ByRef $nrows)
Local $ncols
$res = _SQLite_GetTable2d($hDb, "select user as [Utilisateur], (select total(pages * copies) from log G where L.user = G.user and g.paper = 'A3') as [Pages A3 imprimées], (select total(pages * copies) from log G where L.user = G.user and g.paper = 'A4') as [Pages A4 imprimées] from log L where printer = " & X($imp) & " group by user ;", $rows, $nrows, $ncols)
_ArrayDisplay($rows, "Copies par utilisateur")
EndFunc ;==>ReqBD_BE
Func X($s)
Return ("'" & StringReplace($s, "'", "''", 0, 1) & "'")
EndFunc ;==>X
Func XX($s)
Return (",'" & StringReplace($s, "'", "''", 0, 1) & "'")
EndFunc ;==>XX
Func Y($s)
Return ("X'" & Hex($s) & "'")
EndFunc ;==>Y
Func YY($s)
Return (",X'" & Hex($s) & "'")
EndFunc ;==>YY
Func Z($s)
Return (Number($s))
EndFunc ;==>Z
Func ZZ($s)
Return ("," & Number($s))
EndFunc ;==>ZZ