⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lmsensors.c

📁 snmp的源代码,已经在我的ubuntu下编译通过
💻 C
📖 第 1 页 / 共 3 页
字号:
           /* look for children of children (note, this is recursive) */         if (process_sensors(childh) == PICL_SUCCESS) {            return (PICL_SUCCESS);        }          /* get next child node at this level*/        error_code = picl_get_propval_by_name(childh, PICL_PROP_PEER,                                        &nexth, sizeof (picl_nodehdl_t));        if (error_code != PICL_SUCCESS) {/* no more children - buh bye*/            return (error_code);        }        childh = nexth;    } /* while */    return (error_code);} /* process sensors */static intget_child(picl_nodehdl_t nodeh, char *cname, picl_nodehdl_t *resulth){    picl_nodehdl_t  childh;    picl_nodehdl_t  nexth;    char            pname[PICL_PROPNAMELEN_MAX];    picl_errno_t    error_code;    /* look up first child node */    error_code = picl_get_propval_by_name(nodeh, PICL_PROP_CHILD, &childh,                                        sizeof (picl_nodehdl_t));    if (error_code != PICL_SUCCESS) {            return (error_code);    }    /* step through child nodes, get the name first */    while (error_code == PICL_SUCCESS) {        error_code = picl_get_propval_by_name(childh, PICL_PROP_NAME,                                              pname, (PICL_PROPNAMELEN_MAX - 1));        if (error_code != PICL_SUCCESS) {  /*we found a node with no name.  Impossible.! */            return (error_code);        }        if (strncmp(pname, cname,PICL_PROPNAMELEN_MAX) == 0){            *resulth = childh;            return (PICL_SUCCESS);        }        /* look for children of children (note, this is recursive) */        if (get_child(childh,cname,resulth) == PICL_SUCCESS) {             return (PICL_SUCCESS);        }        /* get next child node at this level*/                    error_code = picl_get_propval_by_name(childh, PICL_PROP_PEER,                                        &nexth, sizeof (picl_nodehdl_t));        if (error_code != PICL_SUCCESS) {/* no more children - buh bye*/            return (error_code);        }        childh = nexth;    } /* while */    return (error_code);} /* get child */#endif/* ******** end of picld sensor procedures * */#endif /* solaris2 */static void_sensor_load(clock_t t){#ifdef solaris2    int i,j;    int typ;    int temp;    int other;    int er_code;    char *fantypes[]={"CPU","PWR","AFB"};    kstat_ctl_t *kc;    kstat_t *kp;    envctrl_fan_t *fan_info;    envctrl_ps_t *power_info;    envctrl_encl_t *enc_info;#ifdef HAVE_PICL_H    picl_errno_t     error_code;    picl_nodehdl_t  rooth,plath;    char sname[PICL_PROPNAMELEN_MAX] = "SYSTEM";#endif /* DEBUGMSG(("ucd-snmp/lmSensors", "Reading the sensors\n")); *//* initialize the array */    for (i = 0; i < N_TYPES; i++){        sensor_array[i].n = 0;        for (j=0; j < MAX_SENSORS; j++){            sensor_array[i].sensor[j].name[0] = '\0';            sensor_array[i].sensor[j].value = 0;             }        } /*end for i*//* try picld (if supported), if that doesn't work, try kstat */#ifdef HAVE_PICL_H er_code = picl_initialize();if (er_code == PICL_SUCCESS) {    error_code = picl_get_root(&rooth);    if (error_code != PICL_SUCCESS) {        DEBUGMSG(("ucd-snmp/lmSensors", "picld couldn't get root error code->%d\n",error_code));        }    else{        error_code = get_child(rooth,sname,&plath);        if (error_code == PICL_SUCCESS){            error_code = process_sensors(plath);            if (error_code != 255)                 if (error_code != 7)                    DEBUGMSG(("ucd-snmp/lmSensors", "picld had an internal problem error code->%d\n",error_code));            } /* endif error_code */        else{            DEBUGMSG(("ucd-snmp/lmSensors", "picld couldn't get system tree error code->%d\n",error_code));            } /* end else error_code */        } /* end else */    picl_shutdown();}  /* end if err_code for picl_initialize */else{  /* try kstat instead */    DEBUGMSG(("ucd-snmp/lmSensors", "picld couldn't initialize picld because error code->%d\n",er_code));#endif  /* end of picld section *//* initialize kstat */kc = kstat_open();if (kc == 0) {    DEBUGMSG(("ucd-snmp/lmSensors", "couldn't open kstat"));    } /* endif kc */else{    kp = kstat_lookup(kc, ENVCTRL_MODULE_NAME, 0, ENVCTRL_KSTAT_FANSTAT);    if (kp == 0) {        DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't lookup fan kstat"));        } /* endif lookup fans */    else{        if (kstat_read(kc, kp, 0) == -1) {            DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't read fan kstat"));            } /* endif kstatread fan */        else{            typ = 1;            fan_info = (envctrl_fan_t *) kp->ks_data;            sensor_array[typ].n = kp->ks_ndata;            for (i=0; i < kp->ks_ndata; i++){                DEBUGMSG(("ucd-snmp/lmSensors", "found instance %d fan type %d speed %d OK %d bustedfan %d\n",                    fan_info->instance, fan_info->type,fan_info->fanspeed,fan_info->fans_ok,fan_info->fanflt_num));                sensor_array[typ].sensor[i].value = fan_info->fanspeed;                snprintf(sensor_array[typ].sensor[i].name,(MAX_NAME - 1),                   "fan type %s number %d",fantypes[fan_info->type],fan_info->instance);                sensor_array[typ].sensor[i].name[MAX_NAME - 1] = '\0';                fan_info++;                } /* end for fan_info */            } /* end else kstatread fan */        } /* end else lookup fans*/    kp = kstat_lookup(kc, ENVCTRL_MODULE_NAME, 0, ENVCTRL_KSTAT_PSNAME);    if (kp == 0) {        DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't lookup power supply kstat"));        } /* endif lookup power supply */    else{        if (kstat_read(kc, kp, 0) == -1) {            DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't read power supply kstat"));            } /* endif kstatread fan */        else{            typ = 2;            power_info = (envctrl_ps_t *) kp->ks_data;            sensor_array[typ].n = kp->ks_ndata;            for (i=0; i < kp->ks_ndata; i++){                DEBUGMSG(("ucd-snmp/lmSensors", "found instance %d psupply temp %d %dW OK %d share %d limit %d\n",                    power_info->instance, power_info->ps_tempr,power_info->ps_rating,                    power_info->ps_ok,power_info->curr_share_ok,power_info->limit_ok));                sensor_array[typ].sensor[i].value = power_info->ps_tempr;                snprintf(sensor_array[typ].sensor[i].name,(MAX_NAME-1),                         "power supply %d",power_info->instance);                sensor_array[typ].sensor[i].name[MAX_NAME - 1] = '\0';                power_info++;                } /* end for power_info */            } /* end else kstatread power supply */        } /* end else lookup power supplies*/    kp = kstat_lookup(kc, ENVCTRL_MODULE_NAME, 0, ENVCTRL_KSTAT_ENCL);    if (kp == 0) {        DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't lookup enclosure kstat"));        } /* endif lookup enclosure */    else{        if (kstat_read(kc, kp, 0) == -1) {            DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't read enclosure kstat"));            } /* endif kstatread enclosure */        else{            enc_info = (envctrl_encl_t *) kp->ks_data;             temp = 0;            other = 0;            for (i=0; i < kp->ks_ndata; i++){               switch (enc_info->type){               case ENVCTRL_ENCL_FSP:                   DEBUGMSG(("ucd-snmp/lmSensors", "front panel value %d\n",enc_info->value));                   typ = 3; /* misc */                   sensor_array[typ].sensor[other].value = enc_info->value;                   strncpy(sensor_array[typ].sensor[other].name,"FSP",MAX_NAME-1);                   sensor_array[typ].sensor[other].name[MAX_NAME-1]='\0'; /* null terminate */                   other++;                   break;               case ENVCTRL_ENCL_AMBTEMPR:                   DEBUGMSG(("ucd-snmp/lmSensors", "ambient temp %d\n",enc_info->value));                   typ = 0; /* temperature sensor */                   sensor_array[typ].sensor[temp].value = enc_info->value;                   strncpy(sensor_array[typ].sensor[temp].name,"Ambient",MAX_NAME-1);                   sensor_array[typ].sensor[temp].name[MAX_NAME-1]='\0'; /* null terminate */                   temp++;                   break;               case ENVCTRL_ENCL_BACKPLANE4:                   DEBUGMSG(("ucd-snmp/lmSensors", "There is a backplane4\n"));                   typ = 3; /* misc */                   sensor_array[typ].sensor[other].value = enc_info->value;                   strncpy(sensor_array[typ].sensor[other].name,"Backplane4",MAX_NAME-1);                   sensor_array[typ].sensor[other].name[MAX_NAME-1]='\0'; /* null terminate */                   other++;                   break;               case ENVCTRL_ENCL_BACKPLANE8:                   DEBUGMSG(("ucd-snmp/lmSensors", "There is a backplane8\n"));                   typ = 3; /* misc */                   sensor_array[typ].sensor[other].value = enc_info->value;                   strncpy(sensor_array[typ].sensor[other].name,"Backplane8",MAX_NAME-1);                   sensor_array[typ].sensor[other].name[MAX_NAME-1]='\0'; /* null terminate */                   other++;                   break;               case ENVCTRL_ENCL_CPUTEMPR:                   DEBUGMSG(("ucd-snmp/lmSensors", "CPU%d temperature %d\n",enc_info->instance,enc_info->value));                   typ = 0; /* temperature sensor */                   sensor_array[typ].sensor[temp].value = enc_info->value;                   snprintf(sensor_array[typ].sensor[temp].name,MAX_NAME,"CPU%d",enc_info->instance);                   sensor_array[typ].sensor[other].name[MAX_NAME-1]='\0'; /* null terminate */                   temp++;                   break;               default:                   DEBUGMSG(("ucd-snmp/lmSensors", "unknown element instance &d type &d value %d\n",                       enc_info->instance, enc_info->type, enc_info->value));                   break;               } /* end switch */               enc_info++;               } /* end for enc_info */               sensor_array[3].n = other;               sensor_array[0].n = temp;            } /* end else kstatread enclosure */        } /* end else lookup enclosure*/    kstat_close(kc);#ifdef HAVE_PICL_H    } /* end else kc not needed if no picld*/#endif} /* end else kstat */#else /* end solaris2 */    const sensors_chip_name *chip;    const sensors_feature_data *data;    int             chip_nr = 0;    int             i;    for (i = 0; i < N_TYPES; i++)        sensor_array[i].n = 0;    while (chip = sensors_get_detected_chips(&chip_nr)) {	int             a = 0;	int             b = 0;        while (data = sensors_get_all_features(*chip, &a, &b)) {            char           *label = NULL;            double          val;            if ((data->mode & SENSORS_MODE_R) &&                (data->mapping == SENSORS_NO_MAPPING) &&                !sensors_get_label(*chip, data->number, &label) &&                !sensors_get_feature(*chip, data->number, &val)) {                int             type = -1;                float           mul;                _sensor_array  *array;                if (strstr(label, "V")) {                    type = 2;                    mul = 1000.0;                }                if (strstr(label, "fan") || strstr(label, "Fan")) {                    type = 1;                    mul = 1.0;                }                if (strstr(label, "temp") || strstr(label, "Temp")) {                    type = 0;                    mul = 1000.0;                }                if (type == -1) {                    type = 3;                    mul = 1000.0;                }                array = &sensor_array[type];                if (MAX_SENSORS <= array->n) {                    snmp_log(LOG_ERR, "too many sensors. ignoring %s\n", label);                    break;                }                strncpy(array->sensor[array->n].name, label, MAX_NAME);                array->sensor[array->n].value = (int) (val * mul);                DEBUGMSGTL(("sensors","sensor %d, value %d\n",                            array->sensor[array->n].name,                            array->sensor[array->n].value));                array->n++;            }	    if (label) {		free(label);		label = NULL;	    }        }    }#endif /*else solaris2 */    timestamp = t;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -