14 Commits

Author SHA1 Message Date
sebastian.serfling 2e44a7eee5 add tickets controller 2024-08-16 12:57:18 +02:00
sebastian.serfling 94292ab0f7 add main Loop 2024-08-13 07:41:11 +02:00
sebastian.serfling 1ad2fc8c9a add some def to main 2024-08-12 16:03:42 +02:00
sebastian.serfling 293267e70c create Folder latest / move Update out of Version to rename 2024-08-12 15:08:04 +02:00
sebastian.serfling ba564899b9 create Folder latest / move Update out of Version to rename 2024-08-12 15:02:26 +02:00
sebastian.serfling 601f86e2e7 create Folder latest / move Update out of Version to rename 2024-08-12 15:00:01 +02:00
sebastian.serfling 172fe23f65 create Folder latest / move Update out of Version to rename 2024-08-12 14:59:49 +02:00
sebastian.serfling 147c156dda add Folder Services / ADD version.txt = 1.1 2024-08-12 14:42:05 +02:00
sebastian.serfling 2500cf9999 add Folder Services / ADD version.txt = 1.0 2024-08-12 14:08:45 +02:00
sebastian.serfling b341ae404d ADD Powershell Scripts 2024-08-12 11:54:06 +02:00
sebastian.serfling 78a18946da 12 2024-08-12 11:52:28 +02:00
sebastian.serfling aeeb06461d ADD Some Things 2024-08-06 13:42:44 +02:00
sebastian.serfling b0d9d39bc0 change 2024-08-06 12:44:23 +02:00
sebastian.serfling fbe2b009f3 Chnage Layout 2024-08-06 12:43:55 +02:00
33 changed files with 872 additions and 2 deletions
+7
View File
@@ -0,0 +1,7 @@
## Ticketsystem
ZAMMAD_URL = "https://ticket.stines.de/api/v1"
ZAMMAD_API_TOKEN ="1v4XGY7cZpBXSfb4s_tIBbywQjcaDV6q65IXQyVXrrBDqVtmAtLxM5tOqIAp0VXZ"
## API-Server
API_SERVER = "http://api.stines.de:8001"
API_TOKEN = "^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8"
+3
View File
@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
+6
View File
@@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
+7
View File
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Black">
<option name="sdkName" value="Python 3.11" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
</project>
+8
View File
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Agents.iml" filepath="$PROJECT_DIR$/.idea/Agents.iml" />
</modules>
</component>
</project>
Generated
+6
View File
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>
+8
View File
@@ -0,0 +1,8 @@
1. Prüfen der IP Adresse
2. Abfrage am API Server welcher Service
3. Ping aller 10sec senden
4. Controller für Service aller 60sec
Fragen ?
Wie die Daten der Controller bereitgestllt werden -> Einheitliche Definitionen
+1 -2
View File
@@ -1,5 +1,4 @@
# Das ist das Tool für die Agents auf Windows und Linux Basis zum bereitstellen von Lizenzen zur Datenbanl über den API-Server ### Das ist das Tool für die Agents auf Windows und Linux Basis zum bereitstellen von Lizenzen zur Datenbanl über den API-Server Das Tool soll auf Basis von Python entwickelt werden und immer auf neue Versione testen bevor es los läuft
# Das Tool soll auf Basis von Python entwickelt werden und immer auf neue Versione testen bevor es los läuft
### Für die Betriebsystem Agents werden seperate Controller für jede Art von Services erstellt, dazu gehören: ### Für die Betriebsystem Agents werden seperate Controller für jede Art von Services erstellt, dazu gehören:
- Exchange - Exchange
- Terminal-Server - Terminal-Server
+84
View File
@@ -0,0 +1,84 @@
# Definieren Sie den Pfad zur Ergebnisdatei
$timestamp = (Get-Date).ToString("yyyyMMdd_HHmmss")
$outputFile = "C:\Scripte\MailboxLastLogins_$timestamp.csv"
# Definieren Sie den API-Endpoint
$apiUrl = "http://api.stines.de:8001/report"
# Your API key
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("access_token", "^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8")
$ipaddress = Get-NetIPAddress -AddressFamily IPv4 | Where-Object { $_.InterfaceAlias -like "*Ethernet*" } | Select-Object -ExpandProperty IPAddress
# Fügen Sie das Exchange-Management-Snap-In hinzu
if (-not (Get-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010 -ErrorAction SilentlyContinue)) {
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
}
# Ermitteln Sie alle Benutzerpostfächer
$mailboxes = Get-Mailbox -RecipientTypeDetails UserMailbox -ResultSize Unlimited
# Initialisieren Sie eine leere Liste für die Ergebnisse
$results = @()
# Zeitstempel für eine Stunde zuvor
$oneHourAgo = (Get-Date).AddHours(-1)
# Durchlaufen Sie alle Postfächer und sammeln Sie die letzten Login-Informationen
foreach ($mailbox in $mailboxes) {
# Überprüfen, ob das Konto deaktiviert ist
$exchangeUserAccountControl = $mailbox.ExchangeUserAccountControl
if ($exchangeUserAccountControl -eq "AccountDisabled") {
continue
}
# Filter für Admin- und Journalpostfächer
if ($mailbox.UserPrincipalName -eq "$" -or
$mailbox.UserPrincipalName -match "Journal" -or
$mailbox.UserPrincipalName -match "admin") {
continue
}
$mailboxStats = Get-MailboxStatistics -Identity $mailbox.UserPrincipalName
$lastLoginTime = $mailboxStats.LastLogonTime
# Prüfen, ob der letzte Login in der letzten Stunde war
if ($lastLoginTime -ge $oneHourAgo) {
$username = $mailbox.UserPrincipalName.Split('@')[0]
$result = [PSCustomObject]@{
Mailbox = $username
LastLogon = $lastLoginTime
}
$results += $result
# Daten für die API-Anfrage vorbereiten
$data = @{
username = $result.Mailbox
lastaccess = $result.LastLogon.ToString("yyyy-MM-dd HH:mm:ss")
ipaddress = $ipaddress
}
# POST-Anfrage an den API-Server senden
$jsonData = ($data | ConvertTo-Json -Depth 3 | Out-String).Trim()
$utf8Json = [System.Text.Encoding]::UTF8.GetBytes($jsonData)
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Body $utf8Json -ContentType "application/json; charset=utf-8" -Headers $headers
if ($response -eq "True") {
Write-Output "Successfully sent data for $($result.Mailbox)"
} else {
Write-Output "Failed to send data for $($result.Mailbox): $response"
}
}
}
# Ergebnisse in CSV exportieren
$results | Export-Csv -Path $outputFile -NoTypeInformation
# Ergebnisse anzeigen
$results | Format-Table -AutoSize
+117
View File
@@ -0,0 +1,117 @@
import msal
import requests
import pandas as pd
from datetime import datetime
# Konfigurationsvariablen
client_id = '90571c9b-d407-4d2a-aadd-4a523ff85296'
client_secret = 'ryp8Q~qr6LBOUL2G333a.mf-vg5V..ONl7qJTdza'
tenant_id = '9e449aaa-285c-4572-a132-58db027026d0'
api_server_endpoint = "http://api.stines.de:8001/office/post"
# headers = 'access_token':'^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8'
# Die URL für das Token
authority = f'https://login.microsoftonline.com/{tenant_id}'
# Der Scope für die Microsoft Graph API
scope = ['https://graph.microsoft.com/.default']
# MSAL-Instanz erstellen
app = msal.ConfidentialClientApplication(
client_id,
authority=authority,
client_credential=client_secret,
)
# Token erhalten
result = None
result = app.acquire_token_silent(scope, account=None)
if not result:
print("Kein Caching vorhanden, holen Sie ein neues Token.")
result = app.acquire_token_for_client(scopes=scope)
print(result)
if "access_token" in result:
# Token erfolgreich erhalten
access_token = result['access_token']
print(access_token)
# API-Endpunkt für aktive Office-Pakete
endpoint = "https://graph.microsoft.com/v1.0/users?$select=userPrincipalName,assignedLicenses,signInActivity"
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
response = requests.get(endpoint, headers=headers)
if response.status_code == 200:
# Die Antwort als JSON behandeln
data = response.json()
# Extrahieren der Benutzerdaten aus dem JSON
users = data.get('value', [])
# Die `skuId` und `lastNonInteractiveSignInDateTime` extrahieren und hinzufügen
user_list = []
for user in users:
if 'assignedLicenses' in user:
for license in user['assignedLicenses']:
if 'skuId' in license:
user_copy = user.copy()
user_copy['skuId'] = license['skuId']
if user_copy.get('signInActivity'):
sign_in_time = user_copy['signInActivity'].get('lastNonInteractiveSignInDateTime')
if sign_in_time:
user_copy['lastNonInteractiveSignInDateTime'] = datetime.strptime(sign_in_time,'%Y-%m-%dT%H:%M:%SZ').strftime('%Y-%m-%d %H:%M:%S')
else:
user_copy['lastNonInteractiveSignInDateTime'] = None
else:
user_copy['lastNonInteractiveSignInDateTime'] = None
user_list.append(user_copy)
# Filtern der Benutzer, die eine `skuId` haben
users_with_skuId = [user for user in user_list if user['skuId']]
# Konvertieren der Benutzerdaten in ein DataFrame
df = pd.DataFrame(users_with_skuId)
# Pandas Anzeigeoptionen anpassen
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_colwidth', None)
pd.set_option('display.width', 1000)
# Alles nach dem @ im "User Principal Name" entfernen
if 'userPrincipalName' in df.columns:
df['userPrincipalName'] = df['userPrincipalName'].str.split('@').str[0]
# Nur die gewünschten Spalten auswählen und an die API-Server übergeben
selected_columns = df[["userPrincipalName", "skuId", "lastNonInteractiveSignInDateTime"]]
reporting_date = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
for index, row in selected_columns.iterrows():
payload = {
"itemkey": row["skuId"],
"username": row["userPrincipalName"],
"reportingdate": reporting_date,
"lastaccess": row["lastNonInteractiveSignInDateTime"]
}
api_response = requests.post(api_server_endpoint, json=payload, headers= {'access_token':'^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8'})
if api_response.status_code == 200:
print(f"Erfolgreich gesendet: {payload}")
else:
print(f"Fehler beim Senden von {payload}: {api_response.status_code} - {api_response.text}")
else:
print(f"Fehler beim Abrufen der Daten: {response.status_code}")
print(f"Antwort: {response.text}")
else:
print("Fehler beim Abrufen des Tokens")
print(result.get("error"))
print(result.get("error_description"))
print(result.get("correlation_id"))
+88
View File
@@ -0,0 +1,88 @@
# Define the time range for the last 1 day
$startTime = (Get-Date).AddHours(-1)
$endTime = Get-Date
# Define the filter hashtable with the time range
$filterHashTable = @{
LogName = 'Security'
Id = 4624
StartTime = $startTime
EndTime = $endTime
}
# Get all events with ID 4624 from the Security log within the defined time range
$events = Get-WinEvent -FilterHashtable $filterHashTable
# Create a hash table to store the last login event for each user
$userLogins = @{}
# Loop through each event
foreach ($event in $events) {
$eventDetails = [xml]$event.ToXml()
# Extract relevant information
$timeCreated = $event.TimeCreated
$username = $eventDetails.Event.EventData.Data | Where-Object { $_.Name -eq 'TargetUserName' } | Select-Object -ExpandProperty '#text'
$ipaddress = Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet | Select-Object -ExpandProperty IPAddress
$logonType = $eventDetails.Event.EventData.Data | Where-Object { $_.Name -eq 'LogonType' } | Select-Object -ExpandProperty '#text'
# Exclude events not related to remote logins and HealthMailbox
if ($logonType -ne "10" -or $username -like "DWM*" -or $username -like "*UMFD*") {
continue
}
$formattedTimeCreated = $timeCreated.ToString("yyyy-MM-dd HH:mm:ss")
# Store the event if it's the latest one for the user
if (-not $userLogins.ContainsKey($username) -or $userLogins[$username].TimeCreated -lt $timeCreated) {
$userLogins[$username] = [PSCustomObject]@{
lastaccess = $formattedTimeCreated
username = $username
ipaddress = $ipaddress
LogonType = $logonType
}
}
}
# Define the JSON file path with current date and hour
$dateString = (Get-Date).ToString("yyyyMMdd_HH-mm")
$jsonPath = "C:\Scripte\LastLogins_$dateString.json"
# Output the last login event for each user to the JSON file
$userLoginsArray = $userLogins.GetEnumerator() | ForEach-Object {
$_.Value
}
$userLoginsArray | ConvertTo-Json | Set-Content -Path $jsonPath -Encoding UTF8
Write-Output "JSON file created at $jsonPath"
# API endpoint URL
$apiUrl = "http://api.stines.de:8001/report"
# Your API key
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("access_token", "^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8")
# Loop through the collected user logins and send each as a JSON payload to the API
foreach ($userLogin in $userLoginsArray) {
$userLoginObject = $userLogin | Select-Object username, lastaccess, ipaddress
# Convert the user login object to JSON
$jsonPayload = $userLoginObject | ConvertTo-Json -Depth 3
# Encode JSON payload in UTF-8
$utf8JsonPayload = [System.Text.Encoding]::UTF8.GetBytes($jsonPayload)
# Send the JSON payload to the API
try {
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Body $utf8JsonPayload -ContentType 'application/json' -Headers $headers
Write-Output "Sent login data for user $($userLoginObject.Username) to the API. Response: $response"
} catch {
Write-Error "Failed to send login data for user $($userLoginObject.Username). Error: $_"
}
}
Write-Output "Finished sending login data to the API"
+7
View File
@@ -0,0 +1,7 @@
### Beschreibt den Vorgang eines Nextcloud Servers
1. Vorhanden User
2. Vorhanden Gruppen
3. Letzer User Login
4. Belegter Speicher
5. Nextcloud Version
+7
View File
@@ -0,0 +1,7 @@
### Beschreibt den Vorgang eines Exchange Servers
1. Vorhanden Postfächer
2. Verbundene Postfächer (lastaccess)
3. Datenbank Größe / Blegung auf Festplatte
4. Exchange Server Version
5. Zertifikat Ablaufdatum
+7
View File
@@ -0,0 +1,7 @@
### Beschreibt den Vorgang eines Mailcow Servers
1. Vorhanden User
2. Vorhanden Alias
3. Letzer User Login
4. Belegter Speicher
5. Mailcow Version
+6
View File
@@ -0,0 +1,6 @@
### Beschreibt den Vorgang eines RDS Servers
1. Kontroller der User die Online waren / sind
2. Kontrolle der Festplatten Belegung
3. Kontrolle der Profile Belgung der User
4. Update Status
+25
View File
@@ -0,0 +1,25 @@
## Controller sind für jeden Service speziele Abfragen von Daten
### RDS
- User Online
- Profile Order Größen
### Exchange Server
- Postfächer verbunden
- Datenbank Größe
- Zertifikat Ablaufdatum
### AD-Controller
- User vorhanden
- Gruppen vorhanden
### Cloud
- Cloud User
- Cloud Gruppen
- Cloud Speicher belegt
### Mailcow
- E-Mail Accounts vorhanden
- E-Mail Postfach belegung
+96
View File
@@ -0,0 +1,96 @@
# Funktion zum Konvertieren des LastLogonTimestamp in Datum
function Convert-LastLogonTimestamp {
param (
[Parameter(Mandatory = $true)]
[long]$Timestamp
)
$DateTime = [DateTime]::FromFileTime($Timestamp)
return $DateTime
}
# Funktion zum Abrufen der lokalen IP-Adresse
function Get-LocalIPAddress {
$ipAddress = [System.Net.Dns]::GetHostAddresses([System.Net.Dns]::GetHostName()) |
Where-Object { $_.AddressFamily -eq 'InterNetwork' } |
Select-Object -First 1
return $ipAddress.IPAddressToString
}
# Gruppe "Users" abrufen
$groupName = "Reporting"
$group = Get-ADGroup -Filter { Name -eq $groupName }
if ($group -eq $null) {
Write-Error "Gruppe '$groupName' wurde nicht gefunden."
exit
}
# Mitglieder der Gruppe abrufen
$groupMembers = Get-ADGroupMember -Identity $group -Recursive | Where-Object { $_.objectClass -eq 'user' }
# Abrufen der lokalen IP-Adresse
$localIPAddress = Get-LocalIPAddress
# Erstellen einer Hash-Tabelle zum Speichern der letzten Anmeldeinformationen für jeden Benutzer
$userLogins = @{}
# Umwandeln der Benutzerinformationen und Ausgabe zur Konsole
foreach ($member in $groupMembers) {
$user = Get-ADUser -Identity $member.SamAccountName -Properties LastLogonTimestamp,createTimeStamp
$lastLogonDateTime = if ($user.LastLogonTimestamp) { (Convert-LastLogonTimestamp -Timestamp $user.LastLogonTimestamp).ToString("yyyy-MM-dd HH:mm:ss") } else { ($user.createTimeStamp).ToString("yyyy-MM-dd HH:mm:ss") }
$convertedUser = [PSCustomObject]@{
username = $user.SamAccountName
lastaccess = $lastLogonDateTime
ipaddress = $localIPAddress
}
# Ausgabe des konvertierten Benutzers zur Konsole
Write-Output $convertedUser
# Rückgabe des konvertierten Benutzers für JSON-Umwandlung
$userLogins[$user.SamAccountName] = $convertedUser
}
# Define the JSON file path with current date and hour
$dateString = (Get-Date).ToString("yyyyMMdd_HH-mm")
$jsonPath = "C:\Scripte\LastLogins_$dateString.json"
# Output the last login event for each user to the JSON file
$userLoginsArray = $userLogins.GetEnumerator() | ForEach-Object {
$_.Value
}
$userLoginsArray | ConvertTo-Json | Set-Content -Path $jsonPath -Encoding UTF8
Write-Output "JSON file created at $jsonPath"
# API endpoint URL
$apiUrl = "http://api.stines.de:8001/report"
# Your API key
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("access_token", "^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8")
# Loop through the collected user logins and send each as a JSON payload to the API
foreach ($userLogin in $userLoginsArray) {
$userLoginObject = $userLogin | Select-Object username, lastaccess, ipaddress
# Convert the user login object to JSON
$jsonPayload = $userLoginObject | ConvertTo-Json -Depth 3
# Encode JSON payload in UTF-8
$utf8JsonPayload = [System.Text.Encoding]::UTF8.GetBytes($jsonPayload)
# Send the JSON payload to the API
try {
$response = Invoke-RestMethod -Uri $apiUrl -Method Post -Body $utf8JsonPayload -ContentType 'application/json' -Headers $headers
Write-Output "Sent login data for user $($userLoginObject.username) to the API. Response: $response"
} catch {
Write-Error "Failed to send login data for user $($userLoginObject.username). Error: $_"
}
}
Write-Output "Finished sending login data to the API"
View File
+9
View File
@@ -0,0 +1,9 @@
## Abfrage des Eregniss für Userlogin (lastacces)
## Abfrage Gruppen (groups)
## Abfrage Festplatten Belegung (space_used)
## Abfrage Profile Ordner Belegung (services_space_used) -> übergabe item & spaceused
## Abfrage Updates (updates = yes/no)
+7
View File
@@ -0,0 +1,7 @@
## Abfrage des Eregniss für Userlogin (lastacces)
## Abfrage Festplatten Belegung (space_used)
## Abfrage Profile Ordner Belegung (services_space_used) -> übergabe item & spaceused
## Abfrage Updates (updates = yes/no)
+9
View File
@@ -0,0 +1,9 @@
## Abfrage des Eregniss für Userlogin (lastacces)
## Abfrage Gruppen (groups)
## Abfrage Festplatten Belegung (space_used)
## Abfrage Profile Ordner Belegung (services_space_used) -> übergabe item & spaceused
## Abfrage Updates (updates = yes/no)
View File
+7
View File
@@ -0,0 +1,7 @@
## Abfrage des Eregniss für Userlogin (lastacces)
## Abfrage Festplatten Belegung (space_used)
## Abfrage Profile Ordner Belegung (services_space_used) -> übergabe item & spaceused
## Abfrage Updates (updates = yes/no)
+64
View File
@@ -0,0 +1,64 @@
import datetime
import requests
import re
import socket
def get_local_ip():
try:
# Ein temporärer Socket erstellen, um die lokale IP-Adresse zu ermitteln
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Verbindung zu einem öffentlichen DNS-Server herstellen
s.connect(("8.8.8.8", 80))
# Die lokale IP-Adresse aus dem Socket abrufen
local_ip = s.getsockname()[0]
s.close()
return local_ip
except Exception as e:
print(f"Fehler beim Ermitteln der lokalen IP-Adresse: {e}")
return None
# URL des API-Servers
sogo_server = "http://172.19.1.4/api/v1/get/logs/sogo/1000"
sogo_headers = {"x-api-key":"6A039F-CED4C3-D69E9E-A6C713-7EBB6A"}
api_server = "http://api.stines.de:8001/report"
api_headers = {"access_token":"^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8"}
def extract_email_username(log_entry):
# Regex für das Finden der E-Mail-Adresse in der Log-Zeile
match = re.search(r"for user '([^']+)'", log_entry)
if match:
email = match.group(1)
# Entfernen des '@' Zeichens aus der E-Mail-Adresse
username = email.split('@')[0]
return username
else:
return None
# Funktion zum Abrufen von Daten vom API-Server
def fetch_data_from_api(url):
response = requests.get(url,headers=sogo_headers)
if response.status_code == 200:
return response.json()
else:
print(f"Fehler beim Abrufen der Daten: {response.status_code}")
return None
# Abrufen der Daten
data = fetch_data_from_api(sogo_server)
if data:
for entry in data:
log_line = entry.get('message')
if log_line and "successful login" in log_line:
username = extract_email_username(log_line)
if username:
payload ={
"ipaddress": "172.19.1.4",
"username": username,
"lastaccess": datetime.datetime.fromtimestamp(int(entry.get('time'))).strftime('%Y-%m-%d %H:%M:%S')
}
api_response = requests.post(api_server, json=payload, headers=api_headers)
print(api_response.status_code)
print(f"Extrahierter Benutzername: {payload}")
else:
print("Keine Daten gefunden.")
+98
View File
@@ -0,0 +1,98 @@
import requests
import json
from datetime import datetime, timedelta
from dotenv import load_dotenv
import os
# Lade die Umgebungsvariablen aus der .env Datei
load_dotenv()
# Hole die Werte aus der .env Datei
zammad_url = os.getenv('ZAMMAD_URL')
zammad_api_token = os.getenv('ZAMMAD_API_TOKEN')
api_url = os.getenv('API_SERVER')
api_token = os.getenv('API_TOKEN')
headers_zammad = {"Authorization": f"Token token={zammad_api_token}"}
headers_api = {"access_token": f"{api_token}", "Content-Type": "application/json"}
# API Anfrage an Zammad für Organisationen
response_org = requests.get(f"{zammad_url}/organizations", headers=headers_zammad)
# API Anfrage an Zammad für Benutzer
response_user = requests.get(f"{zammad_url}/users", headers=headers_zammad)
if response_org.status_code == 200 and response_user.status_code == 200:
organizations = response_org.json()
users = response_user.json()
# Erstelle ein Dictionary, das die Organisationen nach ID speichert
org_dict = {org['id']: org['domain'] for org in organizations}
# Erstelle ein Dictionary, das die Benutzer nach ID speichert
user_dict = {user['id']: user for user in users}
# Berechne den ersten und letzten Tag des letzten Monats
today = datetime.now()
first_day_last_month = today.replace(day=1) - timedelta(days=1)
first_day_last_month = first_day_last_month.replace(day=1)
last_day_last_month = today.replace(day=1) - timedelta(days=1)
# Formatierung der Daten für den Vergleich
start_date_str = first_day_last_month.strftime('%Y-%m-%dT%H:%M:%SZ')
end_date_str = last_day_last_month.strftime('%Y-%m-%dT%H:%M:%SZ')
# API Anfrage an Zammad für Tickets mit Pagination und Sortierung
params = {
'query': f"last_close_at:[{start_date_str} TO {end_date_str}]",
'sort_by': 'last_close_at',
'order': 'desc',
'per_page': 100,
'page': 1
}
response_tickets = requests.get(f"{zammad_url}/tickets/search", headers=headers_zammad, params=params)
if response_tickets.status_code == 200:
response_data = response_tickets.json()
# Extrahiere die Ticket-IDs und die zugehörigen Ticket-Daten aus assets
ticket_ids = response_data.get('tickets', [])
ticket_assets = response_data.get('assets', {}).get('Ticket', {})
# Filtere die Tickets, extrahiere die benötigten Felder und verknüpfe mit Organisationen und Benutzern
filtered_tickets = [
{
'number': ticket_data.get('number'),
'title': ticket_data.get('title'),
'create_date': datetime.strptime(ticket_data.get('created_at'), '%Y-%m-%dT%H:%M:%S.%fZ').strftime('%Y-%m-%d %H:%M:%S') if ticket_data.get('created_at') else None,
'closed_date': datetime.strptime(ticket_data.get('last_close_at'), '%Y-%m-%dT%H:%M:%S.%fZ').strftime('%Y-%m-%d %H:%M:%S') if ticket_data.get('last_close_at') else None,
'organization_domain': org_dict.get(ticket_data.get('organization_id')),
'time_unit': ticket_data.get('time_unit') if ticket_data.get('time_unit') is not None else "0",
'created_by': user_dict.get(ticket_data.get('created_by_id'), {}).get('login')
}
for ticket_id in ticket_ids
if (ticket_data := ticket_assets.get(str(ticket_id)))
]
# Ausgabe der gefilterten Tickets als JSON-String
json_output = json.dumps(filtered_tickets, indent=4, ensure_ascii=False)
for tickets in filtered_tickets:
# POST-Anfrage an den anderen API-Server
print(tickets)
response_post = requests.post(f"{api_url}/post/tickets", headers=headers_api, json=tickets)
# Überprüfen der Antwort des API-Servers
if response_post.status_code == 200 or response_post.status_code == 201:
print("Data successfully posted.")
else:
print(f"Failed to post data. Status code: {response_post.status_code}")
print(f"Response content: {response_post.content.decode('utf-8')}")
else:
print(f"Failed to retrieve tickets. Status code: {response_tickets.status_code}")
else:
if response_org.status_code != 200:
print(f"Failed to retrieve organizations. Status code: {response_org.status_code}")
if response_user.status_code != 200:
print(f"Failed to retrieve users. Status code: {response_user.status_code}")
+15
View File
@@ -0,0 +1,15 @@
import socket
def get_local_ip():
try:
# Ein temporärer Socket erstellen, um die lokale IP-Adresse zu ermitteln
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Verbindung zu einem öffentlichen DNS-Server herstellen
s.connect(("8.8.8.8", 80))
# Die lokale IP-Adresse aus dem Socket abrufen
local_ip = s.getsockname()[0]
s.close()
return local_ip
except Exception as e:
print(f"Fehler beim Ermitteln der lokalen IP-Adresse: {e}")
return None
+74
View File
@@ -0,0 +1,74 @@
import time
import update_check
import os
import platform
import subprocess
import requests
import functions.ipaddress
api_server = "http://api.stines.de:8001/"
api_key = "^YWUbG7yX*V!tV^KBSd*2c&vdN3wV9a2i7f3hfGFMBYFxi6#mMiJGiaA5KEHE%B*miK%qb7rQ67gmcYP@gqmux8"
headers = {"Content-Type":"application/json",
"access_token":f"{api_key}"}
service_ID = []
def restart_service():
check_os = platform.system()
if check_os == ("Linux"):
os.popen("systemctl restart agents.services")
print("Yes Man!")
else:
subprocess.run('sc', 'start', 'Agents')
def get_response(endpoint,data):
response = requests.get(f"{api_server}{endpoint}",headers=headers, json=data)
print(response.text)
return response.text
def post_response(endpoint, data):
response = requests.post(f"{api_server}{endpoint}",headers=headers, json=data)
print(response.text)
return response.text
def check_update():
if update_check.check_version() == True:
restart_service()
post_response("update_checked",{"ipaddress":f'{functions.ipaddress.get_local_ip()}',"version": update_check.current_version})
def ping():
post_response("ping",{"ipaddress":f'{functions.ipaddress.get_local_ip()}'})
def controller_systeminfo():
## Check RAM
## Check CPU
## Check HDD
print("controller.systeminfo()")
def services_run():
service_ID = get_response("get/service",{"ipaddress":f'{functions.ipaddress.get_local_ip()}'})
print(service_ID)
def main_loop():
tasks = [
{"interval": 30, "last_run": time.time(), "functions": [ping,services_run]},
{"interval": 60, "last_run": time.time(), "functions": [controller_systeminfo]},
{"interval": 3600, "last_run": time.time(), "functions": [services_run]}
]
while True:
current_time = time.time()
for task in tasks:
if current_time - task["last_run"] >= task["interval"]:
for function in task["functions"]:
function()
task["last_run"] = current_time
# Eine kurze Pause, um die CPU nicht zu überlasten
time.sleep(1)
if __name__ == "__main__":
main_loop()
View File
+12
View File
@@ -0,0 +1,12 @@
import os
import sys
import requests
current_version = []
new_version = []
# Get Current Version of Files -> Verionfile txt on /opt/agents/version.txt
# Check Verison on Github by Curl http://172.17.1.251/sebastian.serfling/Agents/src/branch/main/version
# IF Version same ->
+41
View File
@@ -0,0 +1,41 @@
import time
def ping():
print("Ping alle 30 Sekunden")
def ping1():
print("Ping1 alle 30 Sekunden")
def ping2():
print("Ping2 alle 60 Sekunden")
def ping3():
print("Ping3 alle 3600 Sekunden")
def main_loop():
tasks = [
{"interval": 3, "last_run": time.time(), "functions": [ping, ping1]},
{"interval": 6, "last_run": time.time(), "functions": [ping2]},
{"interval": 36, "last_run": time.time(), "functions": [ping3]}
]
while True:
current_time = time.time()
for task in tasks:
if current_time - task["last_run"] >= task["interval"]:
for function in task["functions"]:
function()
task["last_run"] = current_time
# Eine kurze Pause, um die CPU nicht zu überlasten
time.sleep(1)
if __name__ == "__main__":
main_loop()
+44
View File
@@ -0,0 +1,44 @@
import os
import sys
import requests
import git
import shutil
def current_version():
current_version = open("version", "r").read()
return current_version
def get_latest_release(owner, repo):
url = f"http://gitlab.stines.de/api/v1/repos/sebastian.serfling/Agents/releases/latest"
response = requests.get(url)
response.raise_for_status()
release_info = response.json()
return release_info['name']
def check_version():
new_version = get_latest_release("", "")
if current_version() != new_version:
try:
git.Repo.clone_from("http://172.17.1.251/sebastian.serfling/Agents.git",f'../{get_latest_release("","")}')
except:
next
if os.path.isdir(f"../{new_version}"):
try:
shutil.move("../latest", f"../{current_version}")
except:
print("folder not Found")
shutil.move(f"../{new_version}", "../latest")
return True
else:
return False
# Get Current Version of Files -> Verionfile txt on /opt/agents/version.txt
# Check Verison on Github by Curl http://172.17.1.251/sebastian.serfling/Agents/src/branch/main/version
# IF Version same -> close
# IF not same -> Update, restart service
+1
View File
@@ -0,0 +1 @@
1.1