lininfo.c

Go to the documentation of this file.
00001 /*
00002 lkmonitor (Linux Kernel Monitor)
00003 
00004 Application for monitoring and tuning a Linux kernel.
00005 
00006 Copyright (C) 2005-2006  Fernando ApesteguĂ­a
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 GNU General Public License for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with this program; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #  include <config.h>
00025 #endif
00026 
00027 #include <gnome.h>
00028 #include <stdio.h>
00029 #include <string.h>
00030 #include <dirent.h>
00031 #include <sys/types.h>
00032 #include <stdlib.h>
00033 #include <glib.h>
00034 
00035 #include "lininfo.h"
00036 #include "others.h"
00037 
00042 int
00043 get_cpu_info (struct cpu_info *cpu)
00044 {
00045   gchar* contents;
00046   char* str;
00047   
00048   if(g_file_get_contents(CPU_FILE,&contents,NULL,NULL)==FALSE)
00049           //Error accesing file. Stop program 
00050         g_error("Can't read %s file",CPU_FILE);
00051   
00052   str=contents;
00053   find_data_for("processor : %[^\n]",cpu->processor,str);
00054   find_data_for("vendor_id : %[^\n]",cpu->vendor_id,str);
00055   find_data_for("cpu family : %[^\n]",cpu->cpu_family,str);
00056   find_data_for("model : %[^\n]",cpu->model,str);
00057   find_data_for("model name : %[^\n]",cpu->model_name,str)      ;
00058   find_data_for("stepping : %[^\n]",cpu->stepping,str);
00059   find_data_for("cpu MHz : %[^\n]",cpu->Mhz,str);
00060   find_data_for("cache size : %[^\n]",cpu->cache,str);
00061   find_data_for("fpu : %[^\n]",cpu->fpu,str);
00062   find_data_for("fpu_exception : %[^\n]",cpu->fpu_exception,str);
00063   find_data_for("cpuid level : %[^\n]",cpu->cpuid_level,str);
00064   find_data_for("wp : %[^\n]",cpu->wp,str);
00065   find_data_for("flags : %[^\n]",cpu->flags,str);
00066   find_data_for("bogomips : %[^\n]",cpu->bogomips,str);
00067         
00068   g_free(contents);
00069 
00070   return (0);
00071 }                               
00072 
00077 int get_mhz_info(char *mhz)
00078 {
00079         gchar *contents;
00080         
00081         if(g_file_get_contents(CPU_FILE,&contents,NULL,NULL)==FALSE){
00082           //Error accesing file. Stop program 
00083                 g_error("Can't read %s file",CPU_FILE);
00084                 return -1;
00085         }
00086         
00087         find_data_for("cpu MHz : %[^\n]",mhz,contents);
00088         
00089         g_free(contents);
00090         
00091         return(0);
00092 }
00093 
00094 
00099 int
00100 get_mem_info (struct mem_info *mem)
00101 {
00102         gchar* contents;
00103         
00104         if(g_file_get_contents(MEM_FILE,&contents,NULL,NULL)==FALSE)
00105                 //Can't read file. Stop program
00106                 g_error("Can't read %s file",MEM_FILE);
00107 
00108   find_data_for ("MemTotal: %[^\n]",mem->total, contents);
00109   find_data_for ("MemFree: %[^\n]",mem->free, contents);
00110   find_data_for ("Buffers: %[^\n]",mem->buffers, contents);
00111   find_data_for ("Cached: %[^\n]",mem->cached, contents);
00112   find_data_for ("SwapCached: %[^\n]",mem->swap_cached, contents);
00113   find_data_for ("Active: %[^\n]",mem->active, contents);
00114   find_data_for ("Inactive: %[^\n]",mem->inactive,contents);
00115   find_data_for ("Dirty: %[^\n]",mem->dirty, contents);
00116   find_data_for ("HighTotal: %[^\n]",mem->high_total, contents);
00117   find_data_for ("HighFree: %[^\n]",mem->high_free, contents);
00118   find_data_for ("LowTotal: %[^\n]",mem->low_total, contents);
00119   find_data_for ("LowFree: %[^\n]",mem->low_free, contents);
00120   find_data_for ("SwapTotal: %[^\n]",mem->swap_total, contents);
00121   find_data_for ("SwapFree: %[^\n]",mem->swap_free, contents);
00122   find_data_for ("HugePages_Total: %[^\n]",mem->hugepages_total, contents);
00123   find_data_for ("HugePages_Free: %[^\n]",mem->hugepages_free, contents);
00124   find_data_for ("Hugepagesize: %[^\n]",mem->hugepages_size, contents);
00125 
00126   g_free(contents);
00127   
00128   return (0);
00129 }                               
00130 
00131 
00136 int get_devs_info(struct dev_info *devs)
00137 {
00138   gchar *contents;
00139   gsize tam;
00140   char *p;
00141 
00142    if(g_file_get_contents(DEV_FILE,&contents,&tam,NULL)==FALSE){
00143             //Can't read file. Stop program
00144                 g_error("Can't read %s file",DEV_FILE);
00145             return -1;
00146    }
00147         
00148     p=strstr(contents,"Block devices:");
00149     *p='\0';
00150     p=skip_lines(p,1);
00151     devs->blocks=g_string_new((gchar*) p);
00152     p=skip_lines(contents,1);
00153     devs->characters=g_string_new(p);
00154    
00155     g_free(contents);
00156    
00157     return(0);
00158 }
00159 
00164 int get_settings_info(struct settings_info *settings)
00165 {
00166         gchar *unk_string=_("Unknown");
00167         gchar *contents;
00168         
00169         if(g_file_get_contents(HOST_NAME,&contents,NULL,NULL)==FALSE){
00170                 g_warning("Failed to read %s file\n",HOST_NAME);
00171                 settings->host=g_string_new(unk_string);
00172                 g_free(contents);
00173                 return (-1);
00174         }else{
00175                 settings->host=g_string_new(contents);
00176                 g_free(contents);
00177         }
00178         
00179         if(g_file_get_contents(DOMAIN_NAME,&contents,NULL,NULL)==FALSE){
00180                 settings->domain=g_string_new(unk_string);
00181                 g_warning("Failed to read %s file\n",DOMAIN_NAME);
00182                 g_free(contents);
00183                 return (-1);
00184         }else{
00185                 settings->domain=g_string_new(contents);
00186                 g_free(contents);               
00187         }
00188         /* We need to read the current values for the settings */
00189         
00190         read_values_from_file(3,FILES,&settings->fdasig,&settings->fdfreed,\
00191         &settings->fdmax);
00192         read_values_from_file(1,THREADS_MAX,&settings->threads);
00193         read_values_from_file(1,P_MAX_Q,&settings->posix_mq);
00194         read_values_from_file(1,P_MAX_M,&settings->posix_mpq);
00195         read_values_from_file(1,P_MAX_S,&settings->posix_ms);
00196         read_values_from_file(2,INODES,&settings->inmax,&settings->inasig);
00197         read_values_from_file(1,PAGE_MAX,&settings->page_max);
00198         read_values_from_file(1,SEG_MAX,&settings->seg_max);
00199         read_values_from_file(1,SEG_SIZE,&settings->seg_size);
00200         read_values_from_file(4,SEM,&settings->max_seminset, &settings->max_semsys,\
00201                                                         &settings->max_opcall, &settings->max_semsets);
00202         
00203         //g_free(contents);
00204         
00205         return(0);
00206 }
00207 
00212 int get_stats_info (struct stats_info *stats)
00213 {
00214         gchar *contents;
00215         gchar str[50];
00216         
00217         if(g_file_get_contents(STATS_FILE,&contents,NULL,NULL)==FALSE){
00218                 //Can't read file.
00219                 g_warning("Failed to read %s file",STATS_FILE);
00220                 g_free(contents);
00221                 stats->cpu=g_string_new("Unknown");
00222                 return -1;
00223         }else{
00224                 find_data_for ("cpu %[^\n]",str, contents);
00225                 stats->cpu=g_string_new(str);
00226                 find_data_for ("cpu0 %[^\n]",str, contents);
00227                 stats->cpu0=g_string_new(str);
00228                 find_data_for ("cpu1 %[^\n]",str, contents);
00229                 stats->cpu1=g_string_new(str);
00230                 find_data_for ("ctxt %[^\n]",str, contents);
00231                 stats->ctxt=g_string_new(str);
00232         }
00233         
00234         g_free(contents);
00235 
00236         return (0);
00237 }
00238 
00239 
00245 int get_battery_info(struct battery_info *battery,const char *file)
00246 {
00247         gchar *contents;
00248         gchar *unk_string=_("Unknown");
00249         int ret=0;
00250         gchar data[10];
00251         
00252         if(g_file_get_contents(file,&contents,NULL,NULL)==FALSE){
00253                 battery->cap=g_string_new(unk_string);
00254                 battery->last_cap=g_string_new(unk_string);
00255                 battery->type=g_string_new(unk_string);
00256                 battery->cap_warning=g_string_new(unk_string);
00257                 battery->cap_low=g_string_new(unk_string);
00258                 
00259                 g_free(contents);
00260                 g_warning("Failed to read %s file\n",file);
00261                 ret=-1;
00262         }else{  
00263                 strcpy(data,parse_batt("design capacity",contents));
00264                 battery->cap=g_string_new(data);
00265                 strcpy(data,parse_batt("last full capacity",contents));
00266                 battery->last_cap=g_string_new(data);
00267                 strcpy(data,parse_batt("design capacity warning",contents));
00268                 battery->cap_warning=g_string_new(data);
00269                 strcpy(data,parse_batt("design capacity low",contents));
00270                 battery->cap_low=g_string_new(data);
00271                 strcpy(data,parse_batt("battery type",contents));
00272                 battery->type=g_string_new(data);
00273         }
00274         
00275         g_free(contents);
00276         return ret;
00277 }
00278 
00283 int get_acpi_info(struct acpi_info *acpi)
00284 {
00285         gchar *contents;
00286         int ret=0;
00287         gchar *unk_string=_("Unknown");
00288         gchar data[10];
00289         
00290         //Read the current battery status.
00291         if(g_file_get_contents(BATT_STATE,&contents,NULL,NULL)==FALSE){
00292                 acpi->battery.rem=g_string_new(unk_string);
00293                 g_free(contents);
00294                 g_warning("Failed to read %s file\n",BATT_STATE);
00295                 
00296                 /* Try with BAT1 subdirectory */
00297                 if(g_file_get_contents(BATT_STATE1,&contents,NULL,NULL)==FALSE){
00298                         acpi->battery.rem=g_string_new(unk_string);
00299                         g_warning("Failed to read %s file\n",BATT_STATE1);
00300                         g_free(contents);
00301                         ret=-1;
00302                 }else{  
00303                         strcpy(data,parse_batt("remaining capacity",contents));
00304                         g_free(contents);
00305                         acpi->battery.rem=g_string_new(data);
00306                 }
00307                 
00308                 //ret=-1;
00309         }else{  
00310                 strcpy(data,parse_batt("remaining capacity",contents));
00311                 g_free(contents);
00312                 acpi->battery.rem=g_string_new(data);
00313         }
00314         
00315         //Read the current temperature.
00316         if(g_file_get_contents(TEMPERATURE,&contents,NULL,NULL)==FALSE){
00317                 acpi->temp=g_string_new(unk_string);
00318                 g_warning("Failed to read %s file\n",TEMPERATURE);
00319                 g_free(contents);
00320                 ret=-1;
00321         }else{  
00322                 //parse_batt also works for this file.
00323                 strcpy(data,parse_batt("temperature",contents));
00324                 g_free(contents);
00325                 acpi->temp=g_string_new(data);
00326         }
00327         
00328         
00329                 //Read the current temperature.
00330         if(g_file_get_contents(TEMP_TRIP,&contents,NULL,NULL)==FALSE){
00331                 acpi->temp_critical=g_string_new(unk_string);
00332                 g_free(contents);
00333                 g_warning("Failed to read %s file\n",TEMP_TRIP);
00334                 ret=-1;
00335         }else{  
00336                 //parse_batt also works for this file.
00337                 strcpy(data,parse_batt("critical",contents));
00338                 g_free(contents);
00339                 acpi->temp_critical=g_string_new(data);
00340         }
00341         
00342         //g_free(contents);
00343         
00344         return(ret);
00345 }

Generated on Sat Dec 2 11:27:51 2006 for lkmonitor by  doxygen 1.5.1