Rule event |
From events scripts available:
Constants:
ScriptFileName - full path to the script
ExeDir - full path to directory of netcom.service.exe without trailing backslash
Objects:
TrafficFilter - methods and properies of this object can be called without name prefix.
CurrentRule - Rule object, containing this event and called from.
VBScript example:
'This script adds record about traffic usage for last hour to database
'after calling this script hour-counters of rule be reseted by service
Dim fso
Dim strTrafficDBPath
strTrafficDBPath = ExeDir & "\Traffic.mdb"
'Check if database file exists. If not exists - create new
TrafficFilter.GlobalLock
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(strTrafficDBPath) Then
CreateJetDatabase strTrafficDBPath
CreateJetTable strTrafficDBPath
End If
Set fso = Nothing
TrafficFilter.GlobalUnlock
'If counters = 0 then do not add any records, else add
If (CurrentRule.LastHourIn > 0) Or (CurrentRule.LastHourOut > 0) Then
AddRecord CurrentRule.Name, CurrentRule.LastHourIn, CurrentRule.LastHourOut
End If
'Adds record to database
Sub AddRecord(argRuleName, argIn, argOut)
Dim Cn, Cmd, strSQLAdd
strSQLAdd = "INSERT INTO Traffic VALUES(NOW(), '" & argRuleName & "', '" & CStr(argIN) & "', '" & CStr(argOut) & "')"
Set Cn = CreateObject("ADODB.Connection")
Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strTrafficDBPath
Set Cmd = CreateObject("ADODB.Command")
Set Cmd.ActiveConnection = Cn
Cmd.CommandText = strSQLAdd
Cmd.Execute
Set Cmd = Nothing
Cn.Close
Set Cn = Nothing
End Sub
'Creates new database file
Sub CreateJetDatabase(strDBPath)
Dim catNewDB
Set catNewDB = CreateObject("ADOX.Catalog")
catNewDB.Create "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strDBPath
Set catNewDB = Nothing
End Sub
'Creates new table in new database
Sub CreateJetTable(strDBPath)
Const adVarWChar = 202
Const adDate = 7
Const adDouble = 5
Const adKeyPrimary = 1
Dim Cn, Cat, objTable
Set Cn = CreateObject("ADODB.Connection")
Set Cat = CreateObject("ADOX.Catalog")
Set objTable = CreateObject("ADOX.Table")
Cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strDBPath
Set Cat.ActiveConnection = Cn
With objTable
.Name = "Traffic"
.Columns.Append "Date and time", adDate
.Columns.Append "Rule name", adVarWChar, 50
.Keys.Append "PrimaryKey", adKeyPrimary, "Rule name"
.Keys("PrimaryKey").Columns.Append "Date and time"
.Columns.Append "In", adDouble
.Columns.Append "Out", adDouble
End With
Cat.Tables.Append objTable
Set objTable = Nothing
Set Cat = Nothing
Cn.Close
Set Cn = Nothing
End Sub
To install this script you must add action to needed rule (see Scripts / programs execution).
Now script can be started every 1 hour for this rule and writes traffic usage of last hour.
Note: in script used Rule.LastHourIn and Rule.LastHourOut properties. Therefore the 1-hour interval of executing script is used!