PDA

View Full Version : System Info



rockom
August 10th, 2010, 03:34 PM
Hello,

With the help of google. I've found info about using lshw to find details on system hardware.


$sudo lshw -html > your-file-name.html

I'm looking for a way to pull this into some C code. I don't want to use lshw. I'd like to use a library of some sort. Something that's already part of the Ubuntu install would be ideal. I'm using C, no C++.

The main details I'm looking for are.
Motherboard, memory, and CPU descriptions.

Any suggestions?

Thanks,
-Rocko

Ferrat
August 10th, 2010, 04:58 PM
For such detection have a look at the /proc folder there you can often get very good information on different parts of the system
/proc/cpuinfo
/proc/meminfo

Motherboard depends on how much info you want, if you just need the name then

sudo dmidecode | grep Product

Should give it to you but you program would have to run that, then put it in a file and read it in to the program, not that much of a hassle but the sudo req sucks

rockom
August 10th, 2010, 08:00 PM
For such detection have a look at the /proc folder there you can often get very good information on different parts of the system
/proc/cpuinfo
/proc/meminfo

Motherboard depends on how much info you want, if you just need the name then

sudo dmidecode | grep Product

Should give it to you but you program would have to run that, then put it in a file and read it in to the program, not that much of a hassle but the sudo req sucks

Thanks for the reply.
I'm familiar with the /proc folders. I'm trying to avoid reading these files. How are they generated by the system? I'm looking for a library of functions that can create these files.

Since my post, I've found (via google) the libgtop-2.0 libraries. They are compiling and I can get cpu frequency and total ram. Still searching for more descriptive hardware definitions like those generated in the /proc folders and by lshw.

Thanks,
-Rocko

wmcbrine
August 10th, 2010, 08:41 PM
There is no reason to avoid reading from /proc. They aren't real files, anyway. AFAIK, they're generated directly by the kernel.

If you want to do what lshw does, get the source of lshw and study it. I dunno, but my guess is that you'll find it reads /proc.

Having a separate API for every little thing is the Windows way. The Unix way is to make everything a (sometimes virtual) file.

WitchCraft
August 11th, 2010, 11:01 PM
apt-get source sysinfo

;)

interval1066
August 11th, 2010, 11:19 PM
Then do something like this:

#include <stdio.h>
#include <linux/unistd.h>
#include <linux/kernel.h>

_syscall1(int, sysinfo, struct sysinfo *, info);

int main(void)
{
struct sysinfo s_info;
int error;

error = sysinfo(&s_info);
printf("code error = %d\n", error);
printf("Uptime = %ds\nLoad: 1 min %d / 5 min %d / 15 min %d\n"
"RAM: total %d / free %d / shared %d\n"
"Memory in buffers = %d\nSwap: total %d / free %d\n"
"Number of processes = %d\n",
s_info.uptime, s_info.loads[0],
s_info.loads[1], s_info.loads[2],
s_info.totalram, s_info.freeram,
s_info.sharedram, s_info.bufferram,
s_info.totalswap, s_info.freeswap,
s_info.procs);
return(0);
}

Regardless of the past comments reading from the /proc filesystem is not the best way. In fact I think direct access from userland is going bye bye.

WitchCraft
August 11th, 2010, 11:35 PM
It follows: CPUinfo

http://www.daniweb.com/forums/thread33551.html

rockom
August 12th, 2010, 08:08 PM
My code so far


void system_info(void)
{
FILE *fp;
int status;
char path[2048];

char *inst_mem_cmd = "cat /proc/meminfo |grep \"MemTotal\"| awk '{print $2}'";
char *inst_cpu_cmd = "cat /proc/cpuinfo | grep \"model name\" | awk '{print $4,$5,$6,$7,$8,$9}'";
char *ethernet_ip_cmd = "ip -f inet addr show eth0 |grep inet | awk '{print $2}'";
char *ethernet_hw_addr_cmd = "ifconfig | grep HWaddr | awk '{print $5}'";
char *hostname_cmd = "hostname";

fp = popen(inst_mem_cmd, "r");
if (fp == NULL)
/* Handle error */;
while (fgets(path, 2048, fp) != NULL)
printf("RAM: %s", path);
status = pclose(fp);
//printf("status: %d\n", status);

fp = popen(inst_cpu_cmd, "r");
if (fp == NULL)
/* Handle error */;
while (fgets(path, 2048, fp) != NULL)
printf("CPU: %s", path);
status = pclose(fp);
//printf("status: %d\n", status);

fp = popen(ethernet_ip_cmd, "r");
if (fp == NULL)
/* Handle error */;
while (fgets(path, 2048, fp) != NULL)
printf("ENET: %s", path);
status = pclose(fp);
//printf("status: %d\n", status);

fp = popen(ethernet_hw_addr_cmd, "r");
if (fp == NULL)
/* Handle error */;
while (fgets(path, 2048, fp) != NULL)
printf("HW_ADDR: %s", path);
status = pclose(fp);
//printf("status: %d\n", status);

fp = popen(hostname_cmd, "r");
if (fp == NULL)
/* Handle error */;
while (fgets(path, 2048, fp) != NULL)
printf("HWaddr: %s", path);
status = pclose(fp);
//printf("status: %d\n", status);

}I'd like to try the sysinfo method next.

Thanks,
-Rocko