Sign in to follow this  
Followers 0
masonje

Log script progress w/o ODBC on local PC

2 posts in this topic

I work at a large company and ran into a situation where my log files being written by AutoIT were having collisions of a sort and logs were either missing entire entries or only part of the entry got written. Asked the new guy what he had done in the past and he gave me a really good idea (and the code for the asp). We setup the SQL server (any one will do, just define it in the asp), set up an asp enabled web server and that is the hardest part a bout this.

Use the _LoggerInitialize() at the beginning of the code and use _Logger() when ever you want to document something in the code.

I setup the INI file to make tweaking the settings as simple and fast as possible. Change the Log_help to 1 or 2 (see code notes) to get the asp call to print to notepad. You don't have to use the ini file. But if you want to make changes on the fly, I would use it (I have learned to cover my butt in this field). Just set up the name of the ini as the same name as the script it self.

You can add or remove as many $Log_Values as you want. It all depends on the fields you have in your DB.

...Enjoy!

#include <date.au3>

_LoggerInitialize()

_Logger("The Cow is running")
_Logger("The Cow jumped over the moon")
_Logger("The Cow landed on his face")
_Logger("Hamburgers for everyone")

Exit

Func _Logger($Log_m)
    ;********* Start Vareables & build URL call *********
    ;add/remove values according to the fields in your DB
    ;the first entire must not have a comma (,) at the beginning
    $Log_Values = $Log_Values & "d=" & _DateTimeFormat(_NowCalc(),2)
    $Log_Values = $Log_Values & ",t=" & @HOUR & ":" & @MIN & ":" & @SEC
    $Log_Values = $Log_Values & ",Script=" & @ScriptName
    $Log_Values = $Log_Values & ",Scriptor=" & $Log_Scriptor
    $Log_Values = $Log_Values & ",LocID=" & @UserName
    $Log_Values = $Log_Values & ",ComputerName=" & @ComputerName
    $Log_Values = $Log_Values & ",OS=" & @OSVersion
    $Log_Values = $Log_Values & ",SPLvl=" & @OSServicePack
    $Log_Values = $Log_Values & ",IP=" & @IPAddress1
    $Log_Values = $Log_Values & ",Msg=" & $Log_m
    ;********* End Vareables & build URL call *********

    ;create final 
    $Log_Send = $Log_URL & "?Table=" & $Log_Table & "&KeyVal=" & $Log_Values

    ;helps print out html call for trouble shooting
    ;set var to 1 in INI file to print
    ;set var to 2 to print and exith w/o URL call
    if $Log_Help <> 0 Then
        TrayTip("", "User input blocked while sending url to notepad", 10)
        BlockInput(1)
        Run("notepad.exe")
        WinWaitActive("Untitled - Notepad")
        Send($Log_Send)
        TrayTip("", "", 0)
        if $Log_Help = 2 Then
            Send("{ENTER}")
            Send("{ENTER}")
            Send("** -- URL net sent, now exiting -- **")
            Exit
        EndIf
        BlockInput(0)
    EndIf

    ;remove any preveous InetGet results
    FileDelete($Log_tempFile)
    ;send info in a URL
    $Log_result=InetGet($Log_Send, $Log_tempFile, 1, 0)
    
    ;check results
    if $Log_result = 0 Then
        ;do something...what I don't know yet
    Else
        ;else it worked...?
        $Log_file = FileOpen($Log_tempFile, 0)
        $Log_chars = FileRead($Log_file)
        if $Log_chars = $Log_GoodResult Then 
            ;MsgBox(0, "good", $Log_chars)
        Else
            ;MsgBox(0, "bad", $Log_chars)
        EndIf
        FileClose($Log_file)
    EndIf
    ;clear out old values
    $Log_Values = ""
EndFunc

Func _LoggerInitialize()
    ;misc var
    Global $Log_Values = "" ;had to set this to make the compiler form complaining
    Global $Log_tempFile = @TempDir & "\logger.txt"

    ;define url from ini or hardcode
    $Log_n = StringSplit(@ScriptName, ".")
    $Log_name = $Log_n[1]
    $Log_iniFile = @ScriptDir & "\" & $Log_name & ".ini"
    if FileExists($Log_iniFile) Then
        ;  http://server.localdomain.com/DBInsert.asp?Table=Log&KeyVal=ComputerName=Jontest,key=....
        Global $Log_URL = IniRead($Log_iniFile, "Setup", "Log_URL", "Null")
        Global $Log_Table = IniRead($Log_iniFile, "Setup", "Log_Table", "Null")
        Global $Log_GoodResult = IniRead($Log_iniFile, "Setup", "Log_GoodResult", "Null")
        Global $Log_Scriptor = IniRead($Log_iniFile, "Setup", "Log_Scriptor", "Null")
        Global $Log_Help=IniRead($Log_iniFile, "Setup", "Log_Help", 2)
    Else
        Global $Log_URL="http://server.localdomain.com/DBInsert.asp"
        Global $Log_Table="Log"
        Global $Log_Scriptor="Default"
        Global $Log_help=0
    EndIf
EndFunc

I Assume you could figure this part...but just in case....Again set the ini file name to the same name as the script it self.

[ini]

[setup]

Log_URL=http://server.localdomain.com/DBInsert.asp

Log_Table=Log

Log_Scriptor=Bob smith

Log_help=0

;Unless the DBInsert.asp changes, don't mess with this one

Log_GoodResult=RecordID=

[/ini]

DBInsert.asp code....

<%

' v1.0
'
' This script is silently called by the logon process to update
' database records with PC information
'
' The web page is called by:
'   http://server.localdomain.com/DBInsert.asp?Table={TABLE}&KeyVal=Key=Val,Key=Val,etc...
'   http://server.localdomain.com/DBInsert.asp?Table=Log&KeyVal=ComputerName=Jontest,key=....
'
' Where {TABLE} = the table name we are adding the record to
'      KeyVal  = a list of attributes (which must exist in the table) and their values

Option Explicit

' ***** Constants
Const CON_STRING = "Driver=SQL Server;Server=server.localdomain.com;Database=DEaS_Log;Uid=SqlID;Pwd=password;"

' ***** Global Vars

Main

Sub Main()
   Dim dictDB
   Dim aPairs, iIndex, aKeyVal


   Set dictDB = Server.CreateObject("Scripting.Dictionary")

   ' Make sure the Table Name looks okay
   If Request.QueryString("Table") = "" Then
      Response.Write "ERROR: Invalid Table specified!"
      Response.End
   End If

   If Request.QueryString("KeyVal") = "" Then
      Response.Write "ERROR: Invalid Key/Value pairs specified!"
      Response.End
   End If

   ' Sort out the KeyVal=Key=Val,Key=Val pairs
   aPairs = Split(Request.QueryString("KeyVal"), ",")
   For iIndex = LBound(aPairs) To UBound(aPairs)
      aKeyVal = Split(aPairs(iIndex), "=")
      If UBound(aKeyVal) = 1 Then
         dictDB(aKeyVal(0)) = aKeyVal(1)
      End If
   Next

   ' Add the key/value pairs we to the table
   AddRecord CON_STRING, CStr(Request.QueryString("Table")), dictDB
End Sub


Function AddRecord(sConString, sTable, dictKeyVal)
   Dim objCon, objRS
   Dim sKey
   

   ' Create DB Connection Objects
   Set objCon = Server.CreateObject("ADODB.Connection")  ' Connection Object
   Set objRS = Server.CreateObject("ADODB.Recordset")   ' Recordset Object

   ' Open Connection
   objCon.Open sConString


   ' SQL Insert command (for reference)
'   objCon.Execute "INSERT INTO LogonTest (ComputerName, MAC) VALUES ('cname1', 'mac1')"

   ' Recordset Insert (using ADO AddNew)
   objRS.Open sTable, objCon, 1, 3, 2  ' Open an updateable recordset
   objRS.AddNew
   For Each sKey in dictKeyVal
      objRS(sKey) = dictKeyVal(sKey)
   Next
   objRS.Update
   
   ' Write out the autonumbered field (returned from the objRS.Update call)
   Response.Write "RecordID=" & objRS("UID")
   Response.End
End Function

%>

Share this post


Link to post
Share on other sites



Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0