107 lines
4.4 KiB
VB.net
107 lines
4.4 KiB
VB.net
Imports System.IO.File
|
|
Imports System.Data.SqlClient
|
|
Imports System.Data.SqlTypes
|
|
Imports System.ComponentModel
|
|
Imports System.IO
|
|
Imports System.SystemException
|
|
Imports System.Reflection
|
|
Imports System.Configuration
|
|
Imports System.ServiceProcess
|
|
|
|
Module Module1
|
|
|
|
Dim Resultstring As String = "Servicechecker Start: " + Format(Now, "dd.MM.yyyy HH:mm:ss") + vbCrLf + vbCrLf
|
|
Sub Main()
|
|
Try
|
|
Dim s As String
|
|
FileOpen(1, System.AppDomain.CurrentDomain.BaseDirectory + "\services.txt", OpenMode.Input)
|
|
While Not EOF(1)
|
|
Input(1, s)
|
|
Console.WriteLine("Check:" + s)
|
|
If Check_Service(s) = False Then
|
|
Resultstring = Resultstring + s + ": " + Chr(9) + "NOK" + vbCrLf
|
|
Console.WriteLine("NOK")
|
|
Else
|
|
Resultstring = Resultstring + s + ": " + Chr(9) + "OK" + vbCrLf
|
|
Console.WriteLine("OK")
|
|
End If
|
|
End While
|
|
'For Each ObjSetting As Configuration.SettingsProperty In My.Settings.Properties
|
|
' If Left(ObjSetting.Name, 7) = "Service" Then
|
|
' Console.WriteLine("Check:" + ObjSetting.DefaultValue)
|
|
' If Check_Service(ObjSetting.DefaultValue) = False Then
|
|
' Resultstring = Resultstring + ObjSetting.DefaultValue + ": " + Chr(9) + "NOK" + vbCrLf
|
|
' Console.WriteLine("NOK")
|
|
' Else
|
|
' Resultstring = Resultstring + ObjSetting.DefaultValue + ": " + Chr(9) + "OK" + vbCrLf
|
|
' Console.WriteLine("OK")
|
|
' End If
|
|
' End If
|
|
'Next
|
|
FileClose(1)
|
|
Catch ex As Exception
|
|
|
|
Resultstring = Resultstring + "Fehler: " + ex.Message + vbCrLf
|
|
Console.WriteLine("NOK: " + ex.Message)
|
|
End Try
|
|
Resultstring = Resultstring + vbCrLf + vbCrLf + "Servicechecker Ende: " + Format(Now, "dd.MM.yyyy HH:mm:ss")
|
|
Sendmail(My.Settings.NOKMail, Resultstring)
|
|
End Sub
|
|
|
|
Public Function Check_Service(ByVal Servicename As String) As Boolean
|
|
Try
|
|
Dim sc As New ServiceController(Servicename)
|
|
|
|
If sc.Status.Equals(ServiceControllerStatus.Stopped) Or sc.Status.Equals(ServiceControllerStatus.StopPending) Then
|
|
Return False
|
|
Else
|
|
Return True
|
|
|
|
End If
|
|
Catch ex As Exception
|
|
Console.WriteLine(ex.Message)
|
|
Return False
|
|
End Try
|
|
|
|
End Function
|
|
|
|
Public Function Sendmail(ByVal email As String, ByVal msg As String) As Boolean
|
|
Dim oread As System.IO.StreamReader
|
|
Console.WriteLine("Sendmail")
|
|
'Mails im Fehler- bzw. im OK-Fall versenden
|
|
oread = IO.File.OpenText(ApplicationPath() + "edokaconn.cfg")
|
|
Dim cstring As String = oread.ReadLine
|
|
oread.Close()
|
|
|
|
Dim meldung As String = msg
|
|
Dim betreff As String = "EDOKA-Service-Checker" + My.Settings.Umgebung
|
|
Dim conn As New SqlConnection(cstring)
|
|
Dim scmCmdToExecute As SqlCommand = New SqlCommand
|
|
scmCmdToExecute.CommandText = "dbo.SP_SendMail"
|
|
scmCmdToExecute.CommandType = CommandType.StoredProcedure
|
|
Dim dtToReturn As DataTable = New DataTable
|
|
scmCmdToExecute.Parameters.Add(New SqlParameter("@email", SqlDbType.VarChar, 255, ParameterDirection.Input, True, 10, 0, "", DataRowVersion.Proposed, email))
|
|
scmCmdToExecute.Parameters.Add(New SqlParameter("@betreff", SqlDbType.VarChar, 255, ParameterDirection.Input, True, 10, 0, "", DataRowVersion.Proposed, betreff))
|
|
scmCmdToExecute.Parameters.Add(New SqlParameter("@meldung", SqlDbType.VarChar, 1024, ParameterDirection.Input, True, 10, 0, "", DataRowVersion.Proposed, meldung))
|
|
scmCmdToExecute.Connection = conn
|
|
Sendmail = True
|
|
Try
|
|
conn.Open()
|
|
scmCmdToExecute.ExecuteNonQuery()
|
|
conn.Close()
|
|
Console.WriteLine("Mail gesendet")
|
|
Catch ex As Exception
|
|
Sendmail = False
|
|
Console.WriteLine(ex.Message)
|
|
Finally
|
|
scmCmdToExecute.Dispose()
|
|
End Try
|
|
End Function
|
|
|
|
Public Function ApplicationPath() As String
|
|
'Return Path.GetDirectoryName([Assembly].GetExecutingAssembly().Location)
|
|
Return Path.GetDirectoryName([Assembly].GetEntryAssembly().Location) + "\"
|
|
End Function
|
|
|
|
End Module
|