-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Martin Hohmann
committed
Feb 11, 2021
0 parents
commit aa7e13b
Showing
5 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
In config.py sind die Datenbank-Zugriffsdaten anzugeben. | ||
|
||
In prozessliste.txt werden je Zeile ein Suchstring angegeben. Groß- und Kleinschreibung werden ignoriert. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
DBHOST = "ocs" | ||
DBPORT="3307" | ||
DBUSER = "autodesk" | ||
DBPWD = "*******" | ||
DB = "test" | ||
TABLE = "autodesklog" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import psutil | ||
import socket | ||
import mysql.connector | ||
import time | ||
from config import * | ||
|
||
# Datenbank-Objekt anlegen | ||
try: | ||
mydb = mysql.connector.connect( | ||
host=DBHOST, | ||
port = DBPORT, | ||
user=DBUSER, | ||
password=DBPWD, | ||
database=DB | ||
) | ||
except: | ||
print("Datenbankverbindung nicht erfolgreich") | ||
exit() | ||
|
||
mycursor = mydb.cursor() | ||
|
||
# Computername in Date schreiben | ||
host = socket.gethostname() | ||
|
||
# Zu überwachende Prozesse aus Datei einkesen | ||
fobj = open("prozessliste.txt", "r") | ||
prozessListeCheck = [procMon.strip() for procMon in fobj] | ||
fobj.close() | ||
|
||
# Liste der laufenden Prozesse erzeugen | ||
prozessliste = [prozess for prozess in psutil.process_iter(['pid','name','username'])] | ||
|
||
# Schleife über die zu monitorenden Prozesse | ||
for monProc in prozessListeCheck: | ||
# Überwachungsvariable ob Prozess läuft wird zunächst auf False gesetzt | ||
laeuft=False | ||
#Schleife über laufende Prozesse | ||
for proc in prozessliste: | ||
# Vergleich ob der Prozess in einem der Prozesse aufgeführt ist | ||
# um Fehler durch Rechtschreibugn auszuschließen werden beide String in Kleinbuchstaben umgewandelt | ||
if monProc.lower() in str(proc.name()).lower(): | ||
# Wenn der Prozess in der Prozessliste vorhanden ist wird ein Datensatz in die DB geschrieben | ||
|
||
# Wenn Prozess unter anderem Benutzernamen läuft, wird eine Exception geworfen. | ||
# Damit das Skript dennoch weiterläuft, wird ein Dummy Username geschrieben | ||
try: | ||
uname = proc.username() | ||
except: | ||
uname = "_Zugriff verweigert_" | ||
pname = proc.name() | ||
print(pname, " is running") | ||
# SQL Statement um in Datenbank zu schreiben | ||
sql = "INSERT INTO "+TABLE+" (time, host, username, proc, cputime) VALUES (%s, %s, %s, %s, %s)" | ||
val = (time.time(), | ||
host, | ||
uname, | ||
pname, | ||
proc.cpu_times().user | ||
) | ||
mycursor.execute(sql, val) | ||
mydb.commit() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
EXCEL.EXE | ||
ted.exe | ||
chrome |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import sys | ||
from cx_Freeze import setup, Executable | ||
|
||
build_exe_options = {'packages': ["psutil","socket","mysql.connector","time"], | ||
'excludes': ["tkinter"] | ||
} | ||
|
||
base = None | ||
if sys.platform == "win32": | ||
base = "Win32GUI" | ||
|
||
setup(name = "ProcessMonitor", | ||
version = "0.2", | ||
description = "Logt ausgewählte Prozesse in MySQL-DB", | ||
options = {"build.exe": build_exe_options}, | ||
executables = [Executable("main.py", base = base)] | ||
) |