-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventar_4_XML.py
More file actions
104 lines (88 loc) · 3.08 KB
/
Inventar_4_XML.py
File metadata and controls
104 lines (88 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/local/bin/python3
##############################################
#
# Name: Inventar_4_XML.py
#
# Author: Peter Christen
#
# Version: 1.0
#
# Date: 10.06.2017
#
# Purpose: Server Inventar Script
# Kann Daten als XML Datei sichern
#
##############################################
import platform,multiprocessing,os,time,argparse,json,xml.dom.minidom
####Parameter auswerten
parser = argparse.ArgumentParser(description='Liest die Inventardaten des aktuellen Servers aus')
parser.add_argument('-c', action='store_true', help="Daten in CSV-Format ausgeben")
parser.add_argument('-j', action='store_true', help="Daten in Json-Format ausgeben")
parser.add_argument('-l', action='store_true', help="Daten in Klar-Text ausgeben(Default)")
parser.add_argument('-J', action='store_true', help="Daten in ein Json-File schreiben")
parser.add_argument('-X', action='store_true', help="Daten in ein XML-File schreiben")
args = parser.parse_args()
###Variabeln
timestamp=time.time()
inventar={}
inventar_liste=[]
###Funktionen
def getdata():
inventar["name"]=platform.node()
inventar["nrcpu"]=multiprocessing.cpu_count()
inventar["system"]=platform.system()
inventar["release"]=platform.release()
inventar["machine"]=platform.machine()
inventar["timestamp"]=int(timestamp)
inventar_liste.append(inventar)
def showdata():
print ("Server Angaben\n##############")
print ("Name:",inventar["name"])
print ("Nr.CPU:",inventar["nrcpu"])
print ("System:",inventar["system"])
print ("Release:",inventar["release"])
print ("Machine:",inventar["machine"])
print ("Timestamp:",inventar["timestamp"])
def showcsv():
print (inventar["name"]+","+str(inventar["nrcpu"])+","+inventar["system"]+","+inventar["release"]+","+inventar["machine"]+","+str(inventar["timestamp"]))
def show_json():
#Json-Ausgabe erstellen
jsonData = json.dumps(inventar_liste)
print (jsonData)
def write_json_file():
with open('inventar.json', 'w') as outfile:
json.dump(inventar_liste, outfile)
def write_xml_file():
###Variabeln
comment="Server Inventardaten"
###Erste Stuffe erstellen
doc = xml.dom.minidom.Document()
doc.appendChild(doc.createComment(comment))
erste_stuffe = doc.createElement("inventar")
doc.appendChild(erste_stuffe)
for i in inventar_liste:
###Zweite Stuffe erstellen
zweite_stuffe = doc.createElement("server")
zweite_stuffe.setAttribute( "name", i["name"])
erste_stuffe.appendChild(zweite_stuffe)
###Datenelemente erstellen
for name,value in i.items():
# Element erstellen
element_name = doc.createElement(name)
zweite_stuffe.appendChild(element_name)
# Element Wert hinzufügen
element_wert = doc.createTextNode(str(value))
element_name.appendChild(element_wert)
###Daten in File schreiben
doc.writexml( open('inventar.xml', 'w'),
indent=" ",
addindent=" ",
newl='\n')
doc.unlink()
###Daten ausgeben
getdata()
if args.c: showcsv()
elif args.j: show_json()
else: showdata()
if args.J: write_json_file()
if args.X: write_xml_file()