I have come to my wits end with this one.
So I created a script that captures data from the operating system, inserts it into a dictionary. The next step should be for that data to be inserted into a postgres database. However I am getting the following error when the for loop is executed that loops through the data.
/usr/bin/python2.7 /home/gmastrokostas/PycharmProjects/learning/Violent_Python.py Traceback (most recent call last): File "/home/gmastrokostas/PycharmProjects/learning/Violent_Python.py", line 52, in for db_loop in eval(server_info): TypeError: eval() arg 1 must be a string or code object
Below is the postgres table script and also the python script it self. The name of the table is SERVERS and the columns are "hostname", "OS", "RAM", "CPU".
SQL SCRIPT TO CREATE THE TABLE - PostgreSQL 9.4
CREATE TABLE servers
(
hostname character varying(50),
"OS" character varying(25),
"RAM" numeric(4,2),
"CPU" character varying(50)
)
WITH (
OIDS=FALSE
);
ALTER TABLE servers
OWNER TO dbuser;
PYTHON SCRIPT
#!/usr/bin/python
import psutil
import os
import math
import platform
import subprocess
import socket
import psycopg2
from decimal import *
#Used by the convert values function.
factor = 1024
def Host_name():
hostname = socket.gethostname()
return hostname
def OS_make():
#CPU INFORMATION.
#cpu_version = platform.dist()[0]+" "+platform.dist()[1]
cpu_version = platform.dist()[0]
return cpu_version
def Virtual_memory_usage():
cvr_info = psutil.virtual_memory().total
return round(cvr_info, 2)
def convert_values():
cvr_info = Virtual_memory_usage()
i = int(math.log(cvr_info)/math.log(factor))
result = float(cvr_info)/factor**i
return result
def get_processor_info():
if platform.system() == "Windows":
return platform.processor()
elif platform.system() == "Darwin":
return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
elif platform.system() == "Linux":
command = "cat /proc/cpuinfo | grep 'model name' | head -1 | awk -F: '{print $2}'"
return subprocess.check_output(command, shell=True).strip()
return ""
#Adjusting precision and then appending to list information taken from Virtual_memory_usage function
mem_frmt = "{0:.2f}".format(convert_values())
server_info = {'hostname': Host_name(), 'OS':OS_make(), 'RAM':mem_frmt, 'CPU':get_processor_info()}
conn = psycopg2.connect("host='10.0.0.41' dbname='serverinfo' user='dbuser'")
cur = conn.cursor()
for db_loop in eval(server_info):
print db_loop #For testing
cur.execute("""INSERT INTO servers(hostname,OS,RAM,CPU) VALUES('%s','%s','%s','%s')""" % \
(db_loop['hostname'], db_loop['OS'], db_loop['RAM'], db_loop['CPU'])
#FOR TESTING BUT IT DOES NOT WORK EITHER
#cur.execute("""INSERT INTO servers(hostname) VALUES('%s')""" % \
# (db_loop['hostname'])
)
conn.commit()