Exemplo n.º 1
0
unsigned int get_sensor_data(unsigned int _id, struct sensor_data *_data)
{
	int a, b, c, num;
	const sensors_chip_name * chip;
	const sensors_feature * features;
	const sensors_subfeature * subfeatures;

	a = num = 0;
	_data->kind = -1;

	while ((chip = sensors_get_detected_chips(NULL, &a)))
	{
		b = 0;
		while ((features = sensors_get_features(chip, &b)))
		{
			c = 0;
			while ((subfeatures = sensors_get_all_subfeatures(chip, features, &c)))
			{
				if (subfeatures->type == SENSORS_SUBFEATURE_FAN_INPUT)
				{
					if (_id == num)
					{
						_data->id = _id;
						_data->chip = chip->addr;
						_data->sensor = features->number;
						char *label = sensors_get_label(chip, features);
						memcpy(_data->label, label, strlen(label)+1);
						free(label);
						_data->kind = SENSOR_FAN;
  						sensors_get_value(chip, subfeatures->number, &_data->data);
						return 1;
					}
					num++;
				}
					
				if (subfeatures->type == SENSORS_SUBFEATURE_TEMP_INPUT)
				{
					if (_id == num)
					{
						_data->id = _id;
						_data->chip = chip->addr;
						_data->sensor = features->number;
						char *label = sensors_get_label(chip, features);
						memcpy(_data->label, label, strlen(label)+1);
						free(label);
						_data->kind = SENSOR_TEMP;
  						sensors_get_value(chip, subfeatures->number, &_data->data);
						return 1;
					}
					num++;
				}
			}
		}
	}
	
	return 0;
}
Exemplo n.º 2
0
void print_chip_raw(const sensors_chip_name *name)
{
	int a, b, err;
	const sensors_feature *feature;
	const sensors_subfeature *sub;
	char *label;
	double val;

	a = 0;
	while ((feature = sensors_get_features(name, &a))) {
		if (!(label = sensors_get_label(name, feature))) {
			fprintf(stderr, "ERROR: Can't get label of feature "
				"%s!\n", feature->name);
			continue;
		}
		printf("%s:\n", label);
		free(label);

		b = 0;
		while ((sub = sensors_get_all_subfeatures(name, feature, &b))) {
			if (sub->flags & SENSORS_MODE_R) {
				if ((err = sensors_get_value(name, sub->number,
							     &val)))
					fprintf(stderr, "ERROR: Can't get "
						"value of subfeature %s: %s\n",
						sub->name,
						sensors_strerror(err));
				else
					printf("  %s: %.3f\n", sub->name, val);
			} else
				printf("(%s)\n", label);
		}
	}
}
Exemplo n.º 3
0
static void print_chip_energy(const sensors_chip_name *name,
			      const sensors_feature *feature,
			      int label_size)
{
	double val;
	const sensors_subfeature *sf;
	char *label;
	const char *unit;

	if (!(label = sensors_get_label(name, feature))) {
		fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
			feature->name);
		return;
	}
	print_label(label, label_size);
	free(label);

	sf = sensors_get_subfeature(name, feature,
				    SENSORS_SUBFEATURE_ENERGY_INPUT);
	if (sf && get_input_value(name, sf, &val) == 0) {
		scale_value(&val, &unit);
		printf("%6.2f %sJ", val, unit);
	} else
		printf("     N/A");

	printf("\n");
}
Exemplo n.º 4
0
static void print_chip_curr(const sensors_chip_name *name,
			    const sensors_feature *feature,
			    int label_size)
{
	const sensors_subfeature *sf;
	double val;
	char *label;
	struct sensor_subfeature_data sensors[NUM_CURR_SENSORS];
	struct sensor_subfeature_data alarms[NUM_CURR_ALARMS];
	int sensor_count, alarm_count;

	if (!(label = sensors_get_label(name, feature))) {
		fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
			feature->name);
		return;
	}
	print_label(label, label_size);
	free(label);

	sf = sensors_get_subfeature(name, feature,
				    SENSORS_SUBFEATURE_CURR_INPUT);
	if (sf && get_input_value(name, sf, &val) == 0)
		printf("%+6.2f A  ", val);
	else
		printf("     N/A  ");

	sensor_count = alarm_count = 0;
	get_sensor_limit_data(name, feature, current_sensors,
			      sensors, &sensor_count, alarms, &alarm_count);

	print_limits(sensors, sensor_count, alarms, alarm_count, label_size,
		     "%s = %+6.2f A");

	printf("\n");
}
Exemplo n.º 5
0
static PyObject*
get_label(ChipName *self, PyObject *args, PyObject *kwargs)
{
    char *kwlist[] = {"feature", NULL};
    Feature *feature = NULL;

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!", kwlist,
                                     &FeatureType, &feature))
    {
        return NULL;
    }

    char *label = sensors_get_label(&self->chip_name, &feature->feature);

    if (label == NULL)
    {
        PyErr_SetString(SensorsException,
                        "sensors_get_label() returned NULL");
        return NULL;
    }

    PyObject *py_label = PyString_FromString(label);
    free(label);

    return py_label;
}
Exemplo n.º 6
0
static void print_chip_power(const sensors_chip_name *name,
			     const sensors_feature *feature,
			     int label_size)
{
	double val;
	const sensors_subfeature *sf;
	struct sensor_subfeature_data sensors[6];
	struct sensor_subfeature_data alarms[3];
	int sensor_count, alarm_count;
	char *label;
	const char *unit;
	int i;

	if (!(label = sensors_get_label(name, feature))) {
		fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
			feature->name);
		return;
	}
	print_label(label, label_size);
	free(label);

	sensor_count = alarm_count = 0;

	/* Power sensors come in 2 flavors: instantaneous and averaged.
	   To keep things simple, we assume that each sensor only implements
	   one flavor. */
	sf = sensors_get_subfeature(name, feature,
				    SENSORS_SUBFEATURE_POWER_INPUT);
	get_sensor_limit_data(name, feature,
			      sf ? power_inst_sensors : power_avg_sensors,
			      sensors, ARRAY_SIZE(sensors), &sensor_count,
			      alarms, ARRAY_SIZE(alarms), &alarm_count);
	/* Add sensors common to both flavors. */
	get_sensor_limit_data(name, feature,
			      power_common_sensors,
			      sensors, ARRAY_SIZE(sensors), &sensor_count,
			      alarms, ARRAY_SIZE(alarms), &alarm_count);
	if (!sf)
		sf = sensors_get_subfeature(name, feature,
					    SENSORS_SUBFEATURE_POWER_AVERAGE);

	if (sf && get_input_value(name, sf, &val) == 0) {
		scale_value(&val, &unit);
		printf("%6.2f %sW  ", val, unit);
	} else
		printf("     N/A  ");

	for (i = 0; i < sensor_count; i++)
		scale_value(&sensors[i].value, &sensors[i].unit);

	print_limits(sensors, sensor_count, alarms, alarm_count,
		     label_size, "%s = %6.2f %sW");

	printf("\n");
}
Exemplo n.º 7
0
static void print_chip_fan(const sensors_chip_name *name,
			   const sensors_feature *feature,
			   int label_size)
{
	const sensors_subfeature *sf, *sfmin, *sfdiv;
	double val;
	char *label;

	if (!(label = sensors_get_label(name, feature))) {
		fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
			feature->name);
		return;
	}
	print_label(label, label_size);
	free(label);

	sf = sensors_get_subfeature(name, feature,
				    SENSORS_SUBFEATURE_FAN_FAULT);
	if (sf && get_value(name, sf))
		printf("   FAULT");
	else {
		sf = sensors_get_subfeature(name, feature,
					    SENSORS_SUBFEATURE_FAN_INPUT);
		if (sf && get_input_value(name, sf, &val) == 0)
			printf("%4.0f RPM", val);
		else
			printf("     N/A");
	}

	sfmin = sensors_get_subfeature(name, feature,
				       SENSORS_SUBFEATURE_FAN_MIN);
	sfdiv = sensors_get_subfeature(name, feature,
				       SENSORS_SUBFEATURE_FAN_DIV);
	if (sfmin && sfdiv)
		printf("  (min = %4.0f RPM, div = %1.0f)",
		       get_value(name, sfmin),
		       get_value(name, sfdiv));
	else if (sfmin)
		printf("  (min = %4.0f RPM)",
		       get_value(name, sfmin));
	else if (sfdiv)
		printf("  (div = %1.0f)",
		       get_value(name, sfdiv));

	sf = sensors_get_subfeature(name, feature,
				    SENSORS_SUBFEATURE_FAN_ALARM);
	if (sf && get_value(name, sf)) {
		printf("  ALARM");
	}

	printf("\n");
}
Exemplo n.º 8
0
static void
_j4status_sensors_add_feature_temp(J4statusPluginContext *context, const sensors_chip_name *chip, const sensors_feature *feature)
{
    const char *name = _j4status_sensors_get_feature_name(chip, feature);

    const sensors_subfeature *input;

    input = sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
    if ( input == NULL )
    {
        g_warning("No temperature input on chip '%s', skipping", name);
        return;
    }

    J4statusSensorsFeature *sensor_feature;
    sensor_feature = g_new0(J4statusSensorsFeature, 1);
    sensor_feature->section = j4status_section_new(context->core);
    sensor_feature->chip = chip;
    sensor_feature->feature = feature;
    sensor_feature->input = input;
    sensor_feature->max = sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_MAX);
    sensor_feature->crit = sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_CRIT);

    char *label;
    label = sensors_get_label(chip, feature);

    gint64 max_width = strlen("+100.0*C");
    if ( context->config.show_details )
    {
        if ( ( sensor_feature->crit != NULL ) && ( sensor_feature->max != NULL ) )
            max_width += strlen(" (high = +100.0°C, crit = +100.0°C)");
        else if ( sensor_feature->crit != NULL )
            max_width += strlen(" (crit = +100.0°C)");
        else if ( sensor_feature->max != NULL )
            max_width += strlen(" (high = +100.0°C)");
    }

    j4status_section_set_name(sensor_feature->section, "sensors");
    j4status_section_set_instance(sensor_feature->section, name);
    j4status_section_set_label(sensor_feature->section, label);
    j4status_section_set_max_width(sensor_feature->section, -max_width);

    free(label);

    if ( j4status_section_insert(sensor_feature->section) )
        context->sections = g_list_prepend(context->sections, sensor_feature);
    else
        _j4status_sensors_feature_free(sensor_feature);
}
/* If a sensor is 'interesting' to us then return its label, otherwise NULL. */
static char *get_sensor_interesting_label (sensors_chip_name chip, int feature) {
	char *label;

	if (sensors_get_ignored(chip, feature) && 
            (sensors_get_label(chip, feature, &label) == 0)) {
                if (! (strcmp ("alarms", label) == 0 || strncmp ("sensor", label, 6) == 0)) {
                        return label;
                } else {
                        free (label);
                }
        }
        

	return NULL;
}
Exemplo n.º 10
0
Feature::Feature(const sensors_chip_name* sensorsChipName, const sensors_feature* sensorsFeature)
    : mSensorsChipName(sensorsChipName),
      mSensorsFeature(sensorsFeature)
{
    char *featureLabel = nullptr;

    if ((featureLabel = sensors_get_label(mSensorsChipName, mSensorsFeature)))
    {
        mLabel = featureLabel;
        free(featureLabel);
    }

    qDebug() << "Detected feature:" << QString::fromLatin1(sensorsFeature->name)
             << "(" << mLabel << ")";
}
Exemplo n.º 11
0
Feature::Feature(const sensors_chip_name* sensorsChipName, const sensors_feature* sensorsFeature)
    : mSensorsChipName(sensorsChipName),
      mSensorsFeature(sensorsFeature)
{
    char *featureLabel = NULL;

    if ((featureLabel = sensors_get_label(mSensorsChipName, mSensorsFeature)))
    {
        mLabel = featureLabel;
        free(featureLabel);
    }

    qDebug() << "Detected feature:" << QString::fromStdString(std::string(sensorsFeature->name))
             << "(" << QString::fromStdString(mLabel) << ")";
}
Exemplo n.º 12
0
static void
build_sensor_list(void)
{
   const sensors_chip_name *chip;
   const sensors_chip_name *match = 0;
   const sensors_feature *feature;
   int chip_nr = 0;

   char name[256];
   while ((chip = sensors_get_detected_chips(match, &chip_nr))) {
      sensors_snprintf_chip_name(name, sizeof(name), chip);

      /* Get all features and filter accordingly. */
      int fnr = 0;
      while ((feature = sensors_get_features(chip, &fnr))) {
         char *featurename = sensors_get_label(chip, feature);
         if (!featurename)
            continue;

         /* Create a 'current' and 'critical' object pair.
          * Ignore sensor if its not temperature based.
          */
         switch(feature->type) {
         case SENSORS_FEATURE_TEMP:
            create_object(name, featurename, chip, feature,
                          SENSORS_TEMP_CURRENT);
            create_object(name, featurename, chip, feature,
                          SENSORS_TEMP_CRITICAL);
            break;
         case SENSORS_FEATURE_IN:
            create_object(name, featurename, chip, feature,
                          SENSORS_VOLTAGE_CURRENT);
            break;
         case SENSORS_FEATURE_CURR:
            create_object(name, featurename, chip, feature,
                          SENSORS_CURRENT_CURRENT);
            break;
         case SENSORS_FEATURE_POWER:
            create_object(name, featurename, chip, feature,
                          SENSORS_POWER_CURRENT);
            break;
         default:
            break;
         }
         free(featurename);
      }
   }
}
Exemplo n.º 13
0
static int get_label_size(const sensors_chip_name *name)
{
	int i;
	const sensors_feature *iter;
	char *label;
	unsigned int max_size = 11;	/* 11 as minimum label width */

	i = 0;
	while ((iter = sensors_get_features(name, &i))) {
		if ((label = sensors_get_label(name, iter)) &&
		    strlen(label) > max_size)
			max_size = strlen(label);
		free(label);
	}

	/* One more for the colon, and one more to guarantee at least one
	   space between that colon and the value */
	return max_size + 2;
}
Exemplo n.º 14
0
static void print_chip_humidity(const sensors_chip_name *name,
				const sensors_feature *feature,
				int label_size)
{
	char *label;
	const sensors_subfeature *subfeature;
	double humidity;

	subfeature = sensors_get_subfeature(name, feature,
					    SENSORS_SUBFEATURE_HUMIDITY_INPUT);
	if (!subfeature)
		return;

	if ((label = sensors_get_label(name, feature))
	 && !sensors_get_value(name, subfeature->number, &humidity)) {
		print_label(label, label_size);
		printf("%6.1f %%RH\n", humidity);
	}
	free(label);
}
Exemplo n.º 15
0
static void print_chip_intrusion(const sensors_chip_name *name,
				 const sensors_feature *feature,
				 int label_size)
{
	char *label;
	const sensors_subfeature *subfeature;
	double alarm;

	subfeature = sensors_get_subfeature(name, feature,
					    SENSORS_SUBFEATURE_INTRUSION_ALARM);
	if (!subfeature)
		return;

	if ((label = sensors_get_label(name, feature))
	 && !sensors_get_value(name, subfeature->number, &alarm)) {
		print_label(label, label_size);
		printf("%s\n", alarm ? "ALARM" : "OK");
	}
	free(label);
}
Exemplo n.º 16
0
static void print_chip_beep_enable(const sensors_chip_name *name,
				   const sensors_feature *feature,
				   int label_size)
{
	char *label;
	const sensors_subfeature *subfeature;
	double beep_enable;

	subfeature = sensors_get_subfeature(name, feature,
					    SENSORS_SUBFEATURE_BEEP_ENABLE);
	if (!subfeature)
		return;

	if ((label = sensors_get_label(name, feature))
	 && !sensors_get_value(name, subfeature->number, &beep_enable)) {
		print_label(label, label_size);
		printf("%s\n", beep_enable ? "enabled" : "disabled");
	}
	free(label);
}
Exemplo n.º 17
0
static void print_chip_vid(const sensors_chip_name *name,
			   const sensors_feature *feature,
			   int label_size)
{
	char *label;
	const sensors_subfeature *subfeature;
	double vid;

	subfeature = sensors_get_subfeature(name, feature,
					    SENSORS_SUBFEATURE_VID);
	if (!subfeature)
		return;

	if ((label = sensors_get_label(name, feature))
	 && !sensors_get_value(name, subfeature->number, &vid)) {
		print_label(label, label_size);
		printf("%+6.3f V\n", vid);
	}
	free(label);
}
Exemplo n.º 18
0
int init_sensor_data() {

	const sensors_chip_name *chip_name;
	const sensors_feature *feature;
	const sensors_subfeature *subfeature;
	char *label;
	int fan_enabled=FALSE, temperature_enabled=FALSE;
	int a,b,c;

	a=0;
	while ( (chip_name = sensors_get_detected_chips(NULL, &a)) ) {

		b=0;
		while ( (feature = sensors_get_features(chip_name, &b)) ) {

			c=0;
			while ( (subfeature = sensors_get_all_subfeatures(chip_name, feature, &c)) ) {
				label = sensors_get_label(chip_name, feature);
				if ( strcmp(CPU_TEMP, label)==0 ) {
					printf ("Found sensor: %s (cpu temperature)\n", CPU_TEMP);
					sd.chip_name_temp=chip_name;
					sd.number_temp=subfeature->number;
					temperature_enabled=TRUE;
					break;
				}
				if (subfeature->type == SENSORS_SUBFEATURE_FAN_INPUT) {
					printf ("Found FAN sensor\n");
					sd.chip_name_fan=chip_name;
					sd.number_fan=subfeature->number;
					fan_enabled=TRUE;
					break;
				}
			}
		}
	}

	if (fan_enabled==TRUE && temperature_enabled==TRUE)
		return TRUE;
	return FALSE;
}
Exemplo n.º 19
0
int
netsnmp_sensor_arch_load(netsnmp_cache *cache, void *vp) {
    netsnmp_sensor_info        *sp;
    const sensors_chip_name    *chip;
    const sensors_feature      *data;
    const sensors_subfeature   *data2;
    int             chip_nr = 0;

    DEBUGMSGTL(("sensors:arch", "Reload v3 LM Sensors module\n"));
    while ((chip = sensors_get_detected_chips( NULL, &chip_nr))) {
	int             a = 0;

        while ((data = sensors_get_features( chip, &a))) {
            DEBUGMSGTL(("sensors:arch:detail", "get_features (%s, %d)\n", data->name, data->number));
	    int             b = 0;
 

            while ((data2 = sensors_get_all_subfeatures( chip, data, &b))) {
                char           *label = NULL;
                double          val;
                int             type = NETSNMP_SENSOR_TYPE_OTHER;

                DEBUGMSGTL(("sensors:arch:detail", "  get_subfeatures (%s, %d)\n", data2->name, data2->number));
                /*
                 * Check the type of this subfeature,
                 *   concentrating on the main "input" measurements.
                 */
                switch ( data2->type ) {
                case SENSORS_SUBFEATURE_IN_INPUT:
                    type = NETSNMP_SENSOR_TYPE_VOLTAGE_DC;
                    break;
                case SENSORS_SUBFEATURE_FAN_INPUT:
                    type = NETSNMP_SENSOR_TYPE_RPM;
                    break;
                case SENSORS_SUBFEATURE_TEMP_INPUT:
                    type = NETSNMP_SENSOR_TYPE_TEMPERATURE;
                    break;
                case SENSORS_SUBFEATURE_VID:
                    type = NETSNMP_SENSOR_TYPE_VOLTAGE_DC;
                    break;
                default:
                    /* Skip everything other than these basic sensor features - ??? */
                    DEBUGMSGTL(("sensors:arch:detail", "  Skip type %x\n", data2->type));
                    continue;
                }
            
                /*
                 * Get the name and value of this subfeature
                 */
/*
                if (!(label = sensors_get_label(chip, data))) {
                    DEBUGMSGTL(("sensors:arch:detail", "  Can't get name (%s)\n", label));
                    continue;
                }
                if (sensors_get_value(chip, data2->number, &val) < 0) {
                    DEBUGMSGTL(("sensors:arch:detail", "  Can't get value (%f)\n", val));
                    continue;
                }
*/
                if (!(label = sensors_get_label(chip, data)) ||
                     (sensors_get_value(chip, data2->number, &val) < 0)) {
                    DEBUGMSGTL(("sensors:arch:detail", "  Can't get name/value (%s, %f)\n", label, val));
                    free(label);
                    label = NULL;
                    continue;
                }
                DEBUGMSGTL(("sensors:arch:detail", "%s = %f\n", label, val));

                /*
                 * Use this type to create a new sensor entry
                 *  (inserting it in the appropriate sub-containers)
                 */
                sp = sensor_by_name( label, type );
                if ( sp ) {
                    sp->value = val;
                    sp->flags|= NETSNMP_SENSOR_FLAG_ACTIVE;
                }
	        if (label) {
		    free(label);
		    label = NULL;
	        }
            } /* end while data2 */
        } /* end while data */
    } /* end while chip */

    return 0;
}
Exemplo n.º 20
0
static int do_features(const sensors_chip_name *chip,
		       const FeatureDescriptor *feature, int action)
{
	char *label;
	const char *formatted;
	int i, alrm, beep, ret;
	double val[MAX_DATA];

	/* If only scanning, take a quick exit if alarm is off */
	alrm = get_flag(chip, feature->alarmNumber);
	if (alrm == -1)
		return -1;
	if (action == DO_SCAN && !alrm)
		return 0;

	for (i = 0; feature->dataNumbers[i] >= 0; i++) {
		ret = sensors_get_value(chip, feature->dataNumbers[i],
					val + i);
		if (ret) {
			sensorLog(LOG_ERR,
				  "Error getting sensor data: %s/#%d: %s",
				  chip->prefix, feature->dataNumbers[i],
				  sensors_strerror(ret));
			return -1;
		}
	}

	/* For RRD, we don't need anything else */
	if (action == DO_RRD) {
		if (feature->rrd) {
			const char *rrded = feature->rrd(val);

			sprintf(rrdBuff + strlen(rrdBuff), ":%s",
				rrded ? rrded : "U");
		}

		return 0;
	}

	/* For scanning and logging, we need extra information */
	beep = get_flag(chip, feature->beepNumber);
	if (beep == -1)
		return -1;

	formatted = feature->format(val, alrm, beep);
	if (!formatted) {
		sensorLog(LOG_ERR, "Error formatting sensor data");
		return -1;
	}

	/* FIXME: It would be more efficient to store the label at
	 * initialization time.
	 */
	label = sensors_get_label(chip, feature->feature);
	if (!label) {
		sensorLog(LOG_ERR, "Error getting sensor label: %s/%s",
			  chip->prefix, feature->feature->name);
		return -1;
	}

	if (action == DO_READ)
		sensorLog(LOG_INFO, "  %s: %s", label, formatted);
	else
		sensorLog(LOG_ALERT, "Sensor alarm: Chip %s: %s: %s",
			  chipName(chip), label, formatted);

	free(label);

	return 0;
}
static GList *libsensors_plugin_get_sensors(void) {
	const sensors_chip_name *chip_name;
	int i;
        GList *sensors = NULL;

#if SENSORS_API_VERSION < 0x400        
	FILE *file;
        g_debug("%s: using libsensors version < 4", __FUNCTION__);

	/* try to open config file, otherwise try alternate config
	 * file - if neither succeed, exit */
	if ((file = fopen (LIBSENSORS_CONFIG_FILE, "r")) == NULL) {
		if ((file = fopen (LIBSENSORS_ALTERNATIVE_CONFIG_FILE, "r")) == NULL) {
                        g_debug("%s: error opening libsensors config file... ", __FUNCTION__);
			return sensors;
		}
	}
	
	/* at this point should have an open config file, if is not
	 * valid, close file and return */
	if (sensors_init(file) != 0) {
		fclose(file);
                g_debug("%s: error initing libsensors from config file...", __FUNCTION__);
		return sensors;
	}
	fclose(file);

	/* libsensors exposes a number of chips -  ... */
	i = 0;
	while ((chip_name = sensors_get_detected_chips (&i)) != NULL) {
		char *chip_name_string;
		const sensors_feature_data *data;
		int n1 = 0, n2 = 0;
                
		chip_name_string = get_chip_name_string(chip_name);
		
		/* ... each of which has one or more 'features' ... */
		while ((data = sensors_get_all_features (*chip_name, &n1, &n2)) != NULL) { // error
                        // fill in list for us
                        check_sensor_with_data(&sensors, chip_name_string, 
                                               chip_name, &n1, &n2, data);
                }
		g_free (chip_name_string);
	}

#else
        g_debug("%s: using libsensors version >= 4", __FUNCTION__);
        
        int nr = 0;
        if (sensors_init(NULL) != 0) {
                g_debug("%s: error initing libsensors", __FUNCTION__);
                return sensors;
        }
	i = 0;
	while ((chip_name = sensors_get_detected_chips(NULL, &nr)))
        {
                char *chip_name_string, *label;                
                const sensors_subfeature *input_feature;
                const sensors_subfeature *low_feature;
                const sensors_subfeature *high_feature;
		const sensors_feature *main_feature;
                SensorType type;
		gint nr1 = 0;
                gdouble value, low, high;
                gchar *path;
                gboolean visible;
                IconType icon;
                
		chip_name_string = get_chip_name_string(chip_name);
                if (chip_name_string == NULL) {
                        g_debug("%s: %d: error getting name string for sensor: %s\n",
                                        __FILE__, __LINE__, chip_name->path);
                        continue;
                }
                
		while ((main_feature = sensors_get_features(chip_name, &nr1)))
                {
			switch (main_feature->type)
                        {
                        case SENSORS_FEATURE_IN:
				type = VOLTAGE_SENSOR;
				input_feature = sensors_get_subfeature(chip_name, 
                                                                    main_feature, 
                                                                    SENSORS_SUBFEATURE_IN_INPUT);
				low_feature = sensors_get_subfeature(chip_name, 
                                                                     main_feature, 
                                                                     SENSORS_SUBFEATURE_IN_MIN);
				high_feature = sensors_get_subfeature(chip_name, 
                                                                      main_feature, 
                                                                      SENSORS_SUBFEATURE_IN_MAX);
			  	break;
                        case SENSORS_FEATURE_FAN:
				type = FAN_SENSOR;
				input_feature = sensors_get_subfeature(chip_name, 
                                                                    main_feature, 
                                                                    SENSORS_SUBFEATURE_FAN_INPUT);
				low_feature = sensors_get_subfeature(chip_name, 
                                                                     main_feature, 
                                                                     SENSORS_SUBFEATURE_FAN_MIN);
                                // no fan max feature
				high_feature = NULL;
				break;
                        case SENSORS_FEATURE_TEMP:
				type = TEMP_SENSOR;
				input_feature = sensors_get_subfeature(chip_name, 
                                                                    main_feature, 
                                                                    SENSORS_SUBFEATURE_TEMP_INPUT);
				low_feature = sensors_get_subfeature(chip_name, 
                                                                     main_feature, 
                                                                     SENSORS_SUBFEATURE_TEMP_MIN);
				high_feature = sensors_get_subfeature(chip_name, 
                                                                      main_feature, 
                                                                      SENSORS_SUBFEATURE_TEMP_MAX);
				if (!high_feature)
					high_feature = sensors_get_subfeature(chip_name, 
									      main_feature, 
									      SENSORS_SUBFEATURE_TEMP_CRIT);
				break;
                        default:
                                g_debug("%s: %d: error determining type for: %s\n",
                                        __FILE__, __LINE__, chip_name_string);
				continue;
                        }
                        
			if (!input_feature)
                        {
                                g_debug("%s: %d: could not get input subfeature for: %s\n",
                                        __FILE__, __LINE__, chip_name_string);
				continue;
                        }
                        // if still here we got input feature so get label
                        label = sensors_get_label(chip_name, main_feature);
                        if (!label) 
                        {
                                g_debug("%s: %d: error: could not get label for: %s\n",
                                        __FILE__, __LINE__, chip_name_string);
                                continue;
                        }

                        g_assert(chip_name_string && label);
                        
                        icon = get_sensor_icon(type);
                        visible = (type == TEMP_SENSOR ? TRUE : FALSE);
                        sensors_applet_plugin_default_sensor_limits(type, 
                                                                    &low, 
                                                                    &high);                        
                        if (low_feature) {
                                sensors_get_value(chip_name, low_feature->number, &low);
                        }

                        if (high_feature) {
                                sensors_get_value(chip_name, high_feature->number, &high);
                        }
                        
                                
                        if (sensors_get_value(chip_name, input_feature->number, &value) < 0) {
                                g_debug("%s: %d: error: could not get value for input feature of sensor: %s\n",
                                        __FILE__, __LINE__, chip_name_string);
                                free(label);
                                continue;
                        }

                        g_debug("for chip %s (type %s) got label %s and value %f", chip_name_string, 
                                (type == TEMP_SENSOR ? "temp" :
                                 (type == FAN_SENSOR ? "fan" :
                                  (type == VOLTAGE_SENSOR ? "voltage" : "error"))), label, value);

                        path = g_strdup_printf ("sensor://%s/%d", chip_name_string, input_feature->number);
			g_hash_table_insert(hash_table, g_strdup(path), (void *)chip_name);
                        sensors_applet_plugin_add_sensor_with_limits(&sensors,
                                                                     path,
                                                                     label,
                                                                     label,
                                                                     type, 
                                                                     visible,
                                                                     low, 
                                                                     high,
                                                                     icon,
                                                                     DEFAULT_GRAPH_COLOR);
                }
                g_free(chip_name_string);
                
                
        }
#endif

        return sensors;
}
Exemplo n.º 22
0
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;
}
Exemplo n.º 23
0
static void print_chip_temp(const sensors_chip_name *name,
			    const sensors_feature *feature,
			    int label_size)
{
	struct sensor_subfeature_data sensors[NUM_TEMP_SENSORS];
	struct sensor_subfeature_data alarms[NUM_TEMP_ALARMS];
	int sensor_count, alarm_count;
	const sensors_subfeature *sf;
	double val;
	char *label;
	int i;

	if (!(label = sensors_get_label(name, feature))) {
		fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
			feature->name);
		return;
	}
	print_label(label, label_size);
	free(label);

	sf = sensors_get_subfeature(name, feature,
				    SENSORS_SUBFEATURE_TEMP_FAULT);
	if (sf && get_value(name, sf)) {
		printf("   FAULT  ");
	} else {
		sf = sensors_get_subfeature(name, feature,
					    SENSORS_SUBFEATURE_TEMP_INPUT);
		if (sf && get_input_value(name, sf, &val) == 0) {
			get_input_value(name, sf, &val);
			if (fahrenheit)
				val = deg_ctof(val);
			printf("%+6.1f%s  ", val, degstr);
		} else
			printf("     N/A  ");
	}

	sensor_count = alarm_count = 0;
	get_sensor_limit_data(name, feature, temp_sensors,
			      sensors, &sensor_count, alarms, &alarm_count);

	for (i = 0; i < sensor_count; i++) {
		if (fahrenheit)
			sensors[i].value = deg_ctof(sensors[i].value);
		sensors[i].unit = degstr;
	}

	print_limits(sensors, sensor_count, alarms, alarm_count, label_size,
		     "%-4s = %+5.1f%s");

	/* print out temperature sensor info */
	sf = sensors_get_subfeature(name, feature,
				    SENSORS_SUBFEATURE_TEMP_TYPE);
	if (sf) {
		int sens = (int)get_value(name, sf);

		/* older kernels / drivers sometimes report a beta value for
		   thermistors */
		if (sens > 1000)
			sens = 4;

		printf("  sensor = %s", sens == 0 ? "disabled" :
		       sens == 1 ? "CPU diode" :
		       sens == 2 ? "transistor" :
		       sens == 3 ? "thermal diode" :
		       sens == 4 ? "thermistor" :
		       sens == 5 ? "AMD AMDSI" :
		       sens == 6 ? "Intel PECI" : "unknown");
	}
	printf("\n");
}
Exemplo n.º 24
0
HwmonCounter::HwmonCounter(DriverCounter *next, char *const name, const sensors_chip_name *const chip, const sensors_feature *feature) : DriverCounter(next, name), mChip(chip), mFeature(feature), mDuplicate(false) {
    mLabel = sensors_get_label(mChip, mFeature);

    switch (mFeature->type) {
    case SENSORS_FEATURE_IN:
        mTitle = "Voltage";
        mDisplay = "maximum";
        mCounterClass = "absolute";
        mUnit = "V";
        mMultiplier = 0.001;
        mMonotonic = false;
        break;
    case SENSORS_FEATURE_FAN:
        mTitle = "Fan";
        mDisplay = "average";
        mCounterClass = "absolute";
        mUnit = "RPM";
        mMultiplier = 1.0;
        mMonotonic = false;
        break;
    case SENSORS_FEATURE_TEMP:
        mTitle = "Temperature";
        mDisplay = "maximum";
        mCounterClass = "absolute";
        mUnit = "°C";
        mMultiplier = 0.001;
        mMonotonic = false;
        break;
    case SENSORS_FEATURE_POWER:
        mTitle = "Power";
        mDisplay = "maximum";
        mCounterClass = "absolute";
        mUnit = "W";
        mMultiplier = 0.000001;
        mMonotonic = false;
        break;
    case SENSORS_FEATURE_ENERGY:
        mTitle = "Energy";
        mDisplay = "accumulate";
        mCounterClass = "delta";
        mUnit = "J";
        mMultiplier = 0.000001;
        mMonotonic = true;
        break;
    case SENSORS_FEATURE_CURR:
        mTitle = "Current";
        mDisplay = "maximum";
        mCounterClass = "absolute";
        mUnit = "A";
        mMultiplier = 0.001;
        mMonotonic = false;
        break;
    case SENSORS_FEATURE_HUMIDITY:
        mTitle = "Humidity";
        mDisplay = "average";
        mCounterClass = "absolute";
        mUnit = "%";
        mMultiplier = 0.001;
        mMonotonic = false;
        break;
    default:
        logg.logError("Unsupported hwmon feature %i", mFeature->type);
        handleException();
    }

    for (HwmonCounter * counter = static_cast<HwmonCounter *>(next); counter != NULL; counter = static_cast<HwmonCounter *>(counter->getNext())) {
        if (strcmp(mLabel, counter->getLabel()) == 0 && strcmp(mTitle, counter->getTitle()) == 0) {
            mDuplicate = true;
            counter->mDuplicate = true;
            break;
        }
    }
}
 Sensor(const sensors_chip_name * n, const sensors_feature * f) : name(n), 
  feature(f) {
    char * l = sensors_get_label(n, f);
    label = l;
    free(l);
 }
Exemplo n.º 26
0
static int sensors_read (void)
{
	if (sensors_load_conf () != 0)
		return (-1);

#if SENSORS_API_VERSION < 0x400
	for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next)
	{
		double value;
		int status;
		char plugin_instance[DATA_MAX_NAME_LEN];
		char type_instance[DATA_MAX_NAME_LEN];

		status = sensors_get_feature (*fl->chip,
				fl->data->number, &value);
		if (status < 0)
			continue;

		status = sensors_snprintf_chip_name (plugin_instance,
				sizeof (plugin_instance), fl->chip);
		if (status < 0)
			continue;

		sstrncpy (type_instance, fl->data->name,
				sizeof (type_instance));

		sensors_submit (plugin_instance,
				sensor_type_name_map[fl->type],
				type_instance,
				value);
	} /* for fl = first_feature .. NULL */
/* #endif SENSORS_API_VERSION < 0x400 */

#elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
	for (featurelist_t *fl = first_feature; fl != NULL; fl = fl->next)
	{
		double value;
		int status;
		char plugin_instance[DATA_MAX_NAME_LEN];
		char type_instance[DATA_MAX_NAME_LEN];
		char *sensor_label;
		const char *type;

		status = sensors_get_value (fl->chip,
				fl->subfeature->number, &value);
		if (status < 0)
			continue;

		status = sensors_snprintf_chip_name (plugin_instance,
				sizeof (plugin_instance), fl->chip);
		if (status < 0)
			continue;

		if (use_labels)
		{
			sensor_label = sensors_get_label (fl->chip, fl->feature);
			sstrncpy (type_instance, sensor_label, sizeof (type_instance));
			free (sensor_label);
		}
		else
		{
			sstrncpy (type_instance, fl->feature->name,
					sizeof (type_instance));
		}

		if (fl->feature->type == SENSORS_FEATURE_IN)
			type = "voltage";
		else if (fl->feature->type
				== SENSORS_FEATURE_FAN)
			type = "fanspeed";
		else if (fl->feature->type
				== SENSORS_FEATURE_TEMP)
			type = "temperature";
		else if (fl->feature->type
				== SENSORS_FEATURE_POWER)
			type = "power";
		else
			continue;

		sensors_submit (plugin_instance, type, type_instance, value);
	} /* for fl = first_feature .. NULL */
#endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */

	return (0);
} /* int sensors_read */
Exemplo n.º 27
0
HwmonCounter::HwmonCounter(HwmonCounter *next, const sensors_chip_name *chip, const sensors_feature *feature) : next(next), key(getEventKey()), polled(false), readable(false), enabled(false), duplicate(false), chip(chip), feature(feature) {

	int len = sensors_snprintf_chip_name(NULL, 0, chip) + 1;
	char *chip_name = new char[len];
	sensors_snprintf_chip_name(chip_name, len, chip);

	len = snprintf(NULL, 0, "hwmon_%s_%d", chip_name, feature->number) + 1;
	name = new char[len];
	snprintf(name, len, "hwmon_%s_%d", chip_name, feature->number);

	delete [] chip_name;

	label = sensors_get_label(chip, feature);

	switch (feature->type) {
	case SENSORS_FEATURE_IN:
		title = "Voltage";
		input = SENSORS_SUBFEATURE_IN_INPUT;
		display = "maximum";
		counter_class = "absolute";
		unit = "V";
		modifier = 1000;
		monotonic = false;
		break;
	case SENSORS_FEATURE_FAN:
		title = "Fan";
		input = SENSORS_SUBFEATURE_FAN_INPUT;
		display = "average";
		counter_class = "absolute";
		unit = "RPM";
		modifier = 1;
		monotonic = false;
		break;
	case SENSORS_FEATURE_TEMP:
		title = "Temperature";
		input = SENSORS_SUBFEATURE_TEMP_INPUT;
		display = "maximum";
		counter_class = "absolute";
		unit = "°C";
		modifier = 1000;
		monotonic = false;
		break;
	case SENSORS_FEATURE_POWER:
		title = "Power";
		input = SENSORS_SUBFEATURE_POWER_INPUT;
		display = "maximum";
		counter_class = "absolute";
		unit = "W";
		modifier = 1000000;
		monotonic = false;
		break;
	case SENSORS_FEATURE_ENERGY:
		title = "Energy";
		input = SENSORS_SUBFEATURE_ENERGY_INPUT;
		display = "accumulate";
		counter_class = "delta";
		unit = "J";
		modifier = 1000000;
		monotonic = true;
		break;
	case SENSORS_FEATURE_CURR:
		title = "Current";
		input = SENSORS_SUBFEATURE_CURR_INPUT;
		display = "maximum";
		counter_class = "absolute";
		unit = "A";
		modifier = 1000;
		monotonic = false;
		break;
	case SENSORS_FEATURE_HUMIDITY:
		title = "Humidity";
		input = SENSORS_SUBFEATURE_HUMIDITY_INPUT;
		display = "average";
		counter_class = "absolute";
		unit = "%";
		modifier = 1000;
		monotonic = false;
		break;
	default:
		logg->logError(__FILE__, __LINE__, "Unsupported hwmon feature %i", feature->type);
		handleException();
	}

	for (HwmonCounter * counter = next; counter != NULL; counter = counter->getNext()) {
		if (strcmp(label, counter->getLabel()) == 0 && strcmp(title, counter->getTitle()) == 0) {
			duplicate = true;
			counter->duplicate = true;
			break;
		}
	}
}
Exemplo n.º 28
0
static void print_chip_power(const sensors_chip_name *name,
			     const sensors_feature *feature,
			     int label_size)
{
	double val;
	const sensors_subfeature *sf;
	struct sensor_subfeature_data sensors[NUM_POWER_SENSORS];
	struct sensor_subfeature_data alarms[NUM_POWER_ALARMS];
	int sensor_count, alarm_count;
	char *label;
	const char *unit;
	int i;

	if (!(label = sensors_get_label(name, feature))) {
		fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
			feature->name);
		return;
	}
	print_label(label, label_size);
	free(label);

	sensor_count = alarm_count = 0;

	/*
	 * Power sensors come in 2 flavors: instantaneous and averaged.
	 * Most devices only support one flavor, so we try to display the
	 * average power if the instantaneous power attribute does not exist.
	 * If both instantaneous power and average power are supported,
	 * average power is displayed as limit.
	 */
	sf = sensors_get_subfeature(name, feature,
				    SENSORS_SUBFEATURE_POWER_INPUT);
	get_sensor_limit_data(name, feature,
			      sf ? power_inst_sensors : power_avg_sensors,
			      sensors, &sensor_count, alarms, &alarm_count);
	/* Add sensors common to both flavors. */
	get_sensor_limit_data(name, feature, power_common_sensors,
			      sensors, &sensor_count, alarms, &alarm_count);
	if (!sf)
		sf = sensors_get_subfeature(name, feature,
					    SENSORS_SUBFEATURE_POWER_AVERAGE);

	if (sf && get_input_value(name, sf, &val) == 0) {
		scale_value(&val, &unit);
		printf("%6.2f %sW%*s", val, unit, 2 - (int)strlen(unit), "");
	} else
		printf("     N/A  ");

	for (i = 0; i < sensor_count; i++) {
		/*
		 * Unit is W and needs to be scaled for all attributes except
		 * interval, which does not need to be scaled and is reported in
		 * seconds.
		 */
		if (strcmp(sensors[i].name, "interval")) {
			char *tmpstr;

			tmpstr = alloca(4);
			scale_value(&sensors[i].value, &unit);
			snprintf(tmpstr, 4, "%sW", unit);
			sensors[i].unit = tmpstr;
		} else {
			sensors[i].unit = "s";
		}
	}
	print_limits(sensors, sensor_count, alarms, alarm_count,
		     label_size, "%s = %6.2f %s");

	printf("\n");
}
Exemplo n.º 29
0
HwmonCounter::HwmonCounter(DriverCounter *next, char *const name, const sensors_chip_name *chip, const sensors_feature *feature) : DriverCounter(next, name), chip(chip), feature(feature), duplicate(false) {
	label = sensors_get_label(chip, feature);

	switch (feature->type) {
	case SENSORS_FEATURE_IN:
		title = "Voltage";
		display = "maximum";
		counter_class = "absolute";
		unit = "V";
		modifier = 1000;
		monotonic = false;
		break;
	case SENSORS_FEATURE_FAN:
		title = "Fan";
		display = "average";
		counter_class = "absolute";
		unit = "RPM";
		modifier = 1;
		monotonic = false;
		break;
	case SENSORS_FEATURE_TEMP:
		title = "Temperature";
		display = "maximum";
		counter_class = "absolute";
		unit = "°C";
		modifier = 1000;
		monotonic = false;
		break;
	case SENSORS_FEATURE_POWER:
		title = "Power";
		display = "maximum";
		counter_class = "absolute";
		unit = "W";
		modifier = 1000000;
		monotonic = false;
		break;
	case SENSORS_FEATURE_ENERGY:
		title = "Energy";
		display = "accumulate";
		counter_class = "delta";
		unit = "J";
		modifier = 1000000;
		monotonic = true;
		break;
	case SENSORS_FEATURE_CURR:
		title = "Current";
		display = "maximum";
		counter_class = "absolute";
		unit = "A";
		modifier = 1000;
		monotonic = false;
		break;
	case SENSORS_FEATURE_HUMIDITY:
		title = "Humidity";
		display = "average";
		counter_class = "absolute";
		unit = "%";
		modifier = 1000;
		monotonic = false;
		break;
	default:
		logg->logError(__FILE__, __LINE__, "Unsupported hwmon feature %i", feature->type);
		handleException();
	}

	for (HwmonCounter * counter = static_cast<HwmonCounter *>(next); counter != NULL; counter = static_cast<HwmonCounter *>(counter->getNext())) {
		if (strcmp(label, counter->getLabel()) == 0 && strcmp(title, counter->getTitle()) == 0) {
			duplicate = true;
			counter->duplicate = true;
			break;
		}
	}
}
Exemplo n.º 30
0
/* ******** end of picld sensor procedures * */

#endif /* solaris2 */
static int
_sensor_load(time_t t)
{
#ifdef solaris2
    int i,j;
#ifdef HAVE_PICL_H 
    int er_code;
    picl_errno_t     error_code;
    int level=0;
    picl_nodehdl_t  rooth;
#else
    int typ;
    int temp=0; /* do not reset this later, more than one typ has temperatures*/
    int other=0;
    const 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;
#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{
        DEBUGMSGTL(("ucd-snmp/lmSensors", "found root\n"));
        error_code = process_sensors(level,rooth);
        if (error_code != 255) 
            if (error_code != 7)
                DEBUGMSG(("ucd-snmp/lmSensors", "picld had an internal problem error code->%d\n",error_code));
        } /* end else */

    picl_shutdown();

}  /* end if err_code for picl_initialize */

else {  
    DEBUGMSG(("ucd-snmp/lmSensors", "picld couldn't initialize picld because error code->%d\n",er_code));

} /*end else picl_initialize */

#else  /* end of picld section */
/* initialize kstat */

kc = kstat_open();
if (kc == 0) {
    DEBUGMSG(("ucd-snmp/lmSensors", "couldn't open kstat"));
    } /* endif kc */
else{
    temp = 0;
    kp = kstat_lookup(kc, ENVCTRL_MODULE_NAME, 0, ENVCTRL_KSTAT_FANSTAT);
    if (kp == 0) {
        DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't lookup fan kstat\n"));
        } /* 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\n"));
        } /* endif lookup power supply */
    else{
        if (kstat_read(kc, kp, 0) == -1) {
            DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't read power supply kstat\n"));
            } /* endif kstatread fan */
        else{
            typ = 0; /* this is a power supply temperature, not a voltage*/
            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 mC %d %dW OK %d share %d limit %d\n",
                    power_info->instance, power_info->ps_tempr*1000,power_info->ps_rating,
                    power_info->ps_ok,power_info->curr_share_ok,power_info->limit_ok));
                sensor_array[typ].sensor[temp].value = power_info->ps_tempr*1000;
                snprintf(sensor_array[typ].sensor[temp].name,(MAX_NAME-1),
                         "power supply %d",power_info->instance);
                sensor_array[typ].sensor[temp].name[MAX_NAME - 1] = '\0';
                power_info++; /* increment the data structure */
                temp++; /* increment the temperature sensor array element */
                } /* 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\n"));
        } /* endif lookup enclosure */
    else{
        if (kstat_read(kc, kp, 0) == -1) {
            DEBUGMSGTL(("ucd-snmp/lmSensors", "couldn't read enclosure kstat\n"));
            } /* endif kstatread enclosure */
        else{
            enc_info = (envctrl_encl_t *) kp->ks_data; 
            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;
                   strlcpy(sensor_array[typ].sensor[other].name, "FSP",
                           MAX_NAME);
                   other++;
                   break;
               case ENVCTRL_ENCL_AMBTEMPR:
                   DEBUGMSG(("ucd-snmp/lmSensors", "ambient temp mC %d\n",enc_info->value*1000));
                   typ = 0; /* temperature sensor */
                   sensor_array[typ].sensor[temp].value = enc_info->value*1000;
                   strlcpy(sensor_array[typ].sensor[temp].name, "Ambient",
                           MAX_NAME);
                   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;
                   strlcpy(sensor_array[typ].sensor[other].name, "Backplane4",
                           MAX_NAME);
                   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;
                   strlcpy(sensor_array[typ].sensor[other].name, "Backplane8",
                           MAX_NAME);
                   other++;
                   break;
               case ENVCTRL_ENCL_CPUTEMPR:
                   DEBUGMSG(("ucd-snmp/lmSensors", "CPU%d temperature mC %d\n",enc_info->instance,enc_info->value*1000));
                   typ = 0; /* temperature sensor */
                   sensor_array[typ].sensor[temp].value = enc_info->value*1000;
                   snprintf(sensor_array[typ].sensor[temp].name,MAX_NAME,"CPU%d",enc_info->instance);
                   sensor_array[typ].sensor[temp].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);

} /* end else kstat */
#endif

#else /* end solaris2 only ie. ifdef everything else */

    const sensors_chip_name *chip;
    const sensors_feature_data *data;
    int             chip_nr = 0;
    unsigned int    i = 0;

    DEBUGMSG(("ucd-snmp/lmSensors", "=> sensor_load\n"));

    for (i = 0; i < N_TYPES; i++)
    {
        sensor_array[i].n = 0;
        sensor_array[i].current_len = 0;

        /* Malloc the default number of sensors. */
        sensor_array[i].sensor = (_sensor*)malloc(sizeof(_sensor) * DEFAULT_SENSORS);
        if (sensor_array[i].sensor == NULL)
        {
           /* Continuing would be unsafe */
           snmp_log(LOG_ERR, "Cannot malloc sensor array!"); 
           return 1;
        } /* end if */
        sensor_array[i].current_len = DEFAULT_SENSORS;
    } /* end for */

    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 = 0;
                _sensor_array  *array;

                /* The label, as determined for a given chip in sensors.conf,
                 * is used to place each sensor in the appropriate bucket.
                 * Volt, Fan, Temp, and Misc.  If the text being looked for below
                 * is not in the label of a given sensor (e.g., the temp1 sensor
                 * has been labeled 'CPU' and not 'CPU temp') it will end up being
                 * lumped in the MISC bucket. */

                if (strstr(label, "V")) {
                    type = VOLT_TYPE;
                    mul = 1000.0;
                }
                if (strstr(label, "fan") || strstr(label, "Fan")) {
                    type = FAN_TYPE;
                    mul = 1.0;
                }
                if (strstr(label, "temp") || strstr(label, "Temp")) {
                    type = TEMP_TYPE;
                    mul = 1000.0;
                }
                if (type == -1) {
                    type = MISC_TYPE;
                    mul = 1000.0;
                }

                array = &sensor_array[type];
                if ( array->current_len <= array->n) {
                    _sensor* old_buffer = array->sensor;
                    size_t new_size = (sizeof(_sensor) * array->current_len) + (sizeof(_sensor) * DEFAULT_SENSORS);
                    array->sensor = (_sensor*)realloc(array->sensor, new_size);
                    if (array->sensor == NULL)
                    {
                       /* Continuing would be unsafe */
                       snmp_log(LOG_ERR, "too many sensors to fit, and failed to alloc more, failing on %s\n", label);
                       free(old_buffer);
                       old_buffer = NULL;
                       if (label) {
                           free(label);
                           label = NULL;
                       } /* end if label */
                       return 1;
                    } /* end if array->sensor */
                    array->current_len = new_size / sizeof(_sensor);
                    DEBUGMSG(("ucd-snmp/lmSensors", "type #%d increased to %d elements\n", type, (int)array->current_len));
                } /* end if array->current */
                strlcpy(array->sensor[array->n].name, label, MAX_NAME);
                array->sensor[array->n].value = (int) (val * mul);
                DEBUGMSGTL(("sensors","sensor %s, value %d\n",
                            array->sensor[array->n].name,
                            array->sensor[array->n].value));
                array->n++;
            } /* end if data-mode */
	    if (label) {
		free(label);
		label = NULL;
	    } /* end if label */
        } /* end while data */
    } /* end while chip */
    DEBUGMSG(("ucd-snmp/lmSensors", "<= sensor_load\n"));
#endif  /* end else ie. ifdef everything else */
    /* Update the timestamp after a load. */
    timestamp = t;
    return 0;
}