Berikut script C untuk melihat properti CPU, RAM, dan VGA di Linux. Karena informasi hardware berada di berbagai lokasi di sistem Linux, script ini membaca dari beberapa sumber:
Script Lengkap: hardware_info.c
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/sysinfo.h>
#include <dirent.h>
// Fungsi untuk membaca file teks
char* read_file(const char* filename) {
FILE* file = fopen(filename, "r");
if (!file) {
return NULL;
}
fseek(file, 0, SEEK_END);
long length = ftell(file);
fseek(file, 0, SEEK_SET);
char* buffer = malloc(length + 1);
if (buffer) {
fread(buffer, 1, length, file);
buffer[length] = '\0';
}
fclose(file);
return buffer;
}
// Fungsi untuk membaca satu baris dari file
void read_single_line(const char* filename, char* buffer, size_t size) {
FILE* file = fopen(filename, "r");
if (file) {
if (fgets(buffer, size, file)) {
// Hapus newline di akhir
buffer[strcspn(buffer, "\n")] = 0;
}
fclose(file);
} else {
strncpy(buffer, "Tidak tersedia", size);
}
}
// Fungsi untuk menampilkan informasi CPU
void display_cpu_info() {
printf("=== INFORMASI CPU ===\n");
// Baca /proc/cpuinfo
char* cpuinfo = read_file("/proc/cpuinfo");
if (cpuinfo) {
char* model = strstr(cpuinfo, "model name");
if (model) {
model = strchr(model, ':');
if (model) {
model += 2; // Lewati ": "
char* end = strchr(model, '\n');
if (end) *end = '\0';
printf("Model CPU : %s\n", model);
}
}
// Hitung jumlah core
int cores = 0;
char* token = cpuinfo;
while ((token = strstr(token, "processor")) != NULL) {
cores++;
token += 9;
}
printf("Jumlah Core : %d\n", cores);
// Frekuensi CPU
char freq[100];
read_single_line("/sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq", freq, sizeof(freq));
if (strcmp(freq, "Tidak tersedia") != 0) {
float freq_ghz = atof(freq) / 1000000.0;
printf("Frekuensi Max : %.2f GHz\n", freq_ghz);
}
free(cpuinfo);
}
// Architecture
char arch[100];
read_single_line("/proc/sys/kernel/arch", arch, sizeof(arch));
printf("Arsitektur : %s\n", arch);
printf("\n");
}
// Fungsi untuk menampilkan informasi RAM
void display_ram_info() {
printf("=== INFORMASI MEMORI (RAM) ===\n");
struct sysinfo sys_info;
if (sysinfo(&sys_info) == 0) {
// Konversi ke GB
double total_ram = (double)sys_info.totalram * sys_info.mem_unit / (1024 * 1024 * 1024);
double free_ram = (double)sys_info.freeram * sys_info.mem_unit / (1024 * 1024 * 1024);
double used_ram = total_ram - free_ram;
double percent_used = (used_ram / total_ram) * 100;
printf("Total RAM : %.2f GB\n", total_ram);
printf("Digunakan : %.2f GB (%.1f%%)\n", used_ram, percent_used);
printf("Tersedia : %.2f GB\n", free_ram);
printf("Swap Total : %.2f GB\n",
(double)sys_info.totalswap * sys_info.mem_unit / (1024 * 1024 * 1024));
} else {
printf("Error membaca informasi RAM\n");
}
printf("\n");
}
// Fungsi untuk menampilkan informasi VGA/GPU
void display_gpu_info() {
printf("=== INFORMASI VGA/GPU ===\n");
int gpu_count = 0;
// Coba deteksi melalui PCI (direktori /sys/class/drm/)
DIR* dir = opendir("/sys/class/drm/");
if (dir) {
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
// Cari entry yang seperti cardX (misal: card0)
if (strncmp(entry->d_name, "card", 4) == 0 && isdigit(entry->d_name[4])) {
char path[256];
snprintf(path, sizeof(path), "/sys/class/drm/%s/device/vendor", entry->d_name);
char vendor_id[100];
read_single_line(path, vendor_id, sizeof(vendor_id));
// Hanya tampilkan jika bukan vendor 0xffff (virtual)
if (strcmp(vendor_id, "0xffff") != 0) {
gpu_count++;
printf("\nGPU %d:\n", gpu_count);
// Nama device
snprintf(path, sizeof(path), "/sys/class/drm/%s/device/device", entry->d_name);
char device_id[100];
read_single_line(path, device_id, sizeof(device_id));
// Vendor berdasarkan ID
if (strstr(vendor_id, "0x8086") || strcmp(vendor_id, "8086") == 0) {
printf(" Vendor : Intel\n");
} else if (strstr(vendor_id, "0x10de") || strcmp(vendor_id, "10de") == 0) {
printf(" Vendor : NVIDIA\n");
} else if (strstr(vendor_id, "0x1002") || strcmp(vendor_id, "1002") == 0) {
printf(" Vendor : AMD/ATI\n");
} else {
printf(" Vendor ID : %s\n", vendor_id);
}
printf(" Device ID : %s\n", device_id);
// Baca driver yang digunakan
snprintf(path, sizeof(path), "/sys/class/drm/%s/device/uevent", entry->d_name);
char* uevent = read_file(path);
if (uevent) {
char* driver = strstr(uevent, "DRIVER=");
if (driver) {
driver += 7; // Lewati "DRIVER="
char* end = strchr(driver, '\n');
if (end) *end = '\0';
printf(" Driver : %s\n", driver);
}
free(uevent);
}
// Baca model dari modalias
snprintf(path, sizeof(path), "/sys/class/drm/%s/device/modalias", entry->d_name);
char modalias[256];
read_single_line(path, modalias, sizeof(modalias));
// Ekstrak nama model dari modalias
char* model_start = strstr(modalias, "v0000");
if (model_start) {
char model[100];
sscanf(model_start, "v%*[0-9A-F]d%*[0-9A-F]%*[^&]&%[^&]", model);
if (strlen(model) > 0) {
printf(" Model : %s\n", model);
}
}
}
}
}
closedir(dir);
}
// Alternatif: coba lspci melalui popen
if (gpu_count == 0) {
printf("Mencoba deteksi via lspci...\n");
FILE* lspci = popen("lspci | grep -i vga", "r");
if (lspci) {
char line[256];
while (fgets(line, sizeof(line), lspci)) {
// Hapus newline
line[strcspn(line, "\n")] = 0;
// Ekstrak nama GPU
char* colon = strchr(line, ':');
if (colon) {
printf(" %s\n", colon + 2);
gpu_count++;
}
}
pclose(lspci);
}
}
if (gpu_count == 0) {
printf("Tidak ditemukan informasi GPU\n");
}
printf("\n");
}
// Fungsi untuk menampilkan informasi sistem
void display_system_info() {
printf("=== INFORMASI SISTEM ===\n");
// Nama host
char hostname[100];
if (gethostname(hostname, sizeof(hostname)) == 0) {
printf("Hostname : %s\n", hostname);
}
// Kernel version
char kernel_version[100];
read_single_line("/proc/sys/kernel/osrelease", kernel_version, sizeof(kernel_version));
printf("Kernel : %s\n", kernel_version);
// Uptime
FILE* uptime_file = fopen("/proc/uptime", "r");
if (uptime_file) {
double uptime_seconds;
fscanf(uptime_file, "%lf", &uptime_seconds);
fclose(uptime_file);
int days = uptime_seconds / 86400;
int hours = (uptime_seconds - (days * 86400)) / 3600;
int minutes = (uptime_seconds - (days * 86400) - (hours * 3600)) / 60;
printf("Uptime : %d hari, %d jam, %d menit\n", days, hours, minutes);
}
printf("\n");
}
int main() {
printf("========================================\n");
printf(" SYSTEM HARDWARE INFORMATION\n");
printf("========================================\n\n");
// Tampilkan semua informasi
display_system_info();
display_cpu_info();
display_ram_info();
display_gpu_info();
printf("========================================\n");
printf("Informasi diambil dari: /proc, /sys, sysinfo()\n");
printf("========================================\n");
return 0;
}
```
Versi Sederhana (Minimalis)
```c
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
void simple_hardware_info() {
printf("=== INFORMASI HARDWARE SEDERHANA ===\n\n");
// 1. CPU Info
printf("CPU:\n");
FILE* cpuinfo = fopen("/proc/cpuinfo", "r");
if (cpuinfo) {
char line[256];
int cores = 0;
while (fgets(line, sizeof(line), cpuinfo)) {
if (strstr(line, "model name")) {
printf(" Model: %s", strchr(line, ':') + 2);
}
if (strstr(line, "processor")) {
cores++;
}
}
printf(" Cores: %d\n", cores);
fclose(cpuinfo);
}
// 2. RAM Info
printf("\nRAM:\n");
struct sysinfo info;
if (sysinfo(&info) == 0) {
double total_gb = (double)info.totalram * info.mem_unit / (1024*1024*1024);
double free_gb = (double)info.freeram * info.mem_unit / (1024*1024*1024);
printf(" Total: %.2f GB\n", total_gb);
printf(" Free : %.2f GB\n", free_gb);
printf(" Used : %.2f GB\n", total_gb - free_gb);
}
// 3. GPU Info (simple)
printf("\nGPU:\n");
FILE* lspci = popen("lspci | grep -i vga", "r");
if (lspci) {
char gpu_info[256];
if (fgets(gpu_info, sizeof(gpu_info), lspci)) {
printf(" %s", gpu_info);
} else {
printf(" Tidak terdeteksi\n");
}
pclose(lspci);
}
}
int main() {
simple_hardware_info();
return 0;
}
```
Cara Kompilasi dan Menjalankan
```bash
# Kompilasi script lengkap
gcc -o hardware_info hardware_info.c
# Jalankan dengan hak akses root untuk info lengkap
sudo ./hardware_info
# Atau kompilasi versi sederhana
gcc -o simple_hw simple_version.c
./simple_hw
```
Penjelasan Singkat:
1. Informasi CPU:
· Dibaca dari /proc/cpuinfo
· Menampilkan model, jumlah core, dan frekuensi
2. Informasi RAM:
· Menggunakan sysinfo() dari <sys/sysinfo.h>
· Menghitung total, used, dan free memory
3. Informasi VGA/GPU:
· Melihat direktori /sys/class/drm/ untuk kartu grafis
· Alternatif menggunakan perintah lspci
· Mendeteksi vendor (NVIDIA, AMD, Intel)
4. Tambahan:
· Informasi sistem (hostname, kernel, uptime)
· Error handling sederhana
Catatan Penting:
· Script memerlukan hak akses root untuk membaca beberapa file sistem
· Informasi VGA mungkin tidak lengkap tanpa driver yang terpasang
· Untuk sistem embedded atau minimal, beberapa path mungkin tidak tersedia
· Kompilasi dengan: gcc -o hwinfo hwinfo.c
Script ini memberikan gambaran umum tentang hardware system. Untuk informasi yang lebih detail, tools seperti lshw, dmidecode, atau nvidia-smi (untuk NVIDIA) lebih direkomendasikan.
Januari 04, 2026
Posted in: 


0 komentar:
Posting Komentar