コード例 #1
0
ファイル: battery.c プロジェクト: ajdiaz/collectd
static int battery_read(void) /* {{{ */
{
  int status;

  if (query_statefs)
    return battery_read_statefs();

  DEBUG("battery plugin: Trying sysfs ...");
  status = read_sysfs();
  if (status == 0)
    return (0);

  DEBUG("battery plugin: Trying acpi ...");
  status = read_acpi();
  if (status == 0)
    return (0);

  DEBUG("battery plugin: Trying pmu ...");
  status = read_pmu();
  if (status == 0)
    return (0);

  ERROR("battery plugin: All available input methods failed.");
  return (-1);
} /* }}} int battery_read */
コード例 #2
0
ファイル: batbeep.c プロジェクト: RiJo/batbeep
int main(int argc, char **argv) {
    (void) signal(SIGINT, sigint_received);

    settings = hm_create(10, 0.5, 7);
    batt_info = hm_create(10, 0.5, 7);

    read_settings();

    strcpy(acpi, hm_get(settings, "acpi_location"));
    strcpy(acpi_alarm, acpi);
    strcat(acpi_alarm, "/alarm\0");
    strcpy(acpi_info, acpi);
    strcat(acpi_info, "/info\0");
    strcpy(acpi_state, acpi);
    strcat(acpi_state, "/state\0");

    read_acpi();

    /* Handle main arguments */
    struct option opt_list[] = {
        {"beep",    0, NULL, 'b'},
        {"help",    0, NULL, 'h'},
        {"version", 0, NULL, 'v'},
        {"debug",   0, NULL, 'd'},
        {0,0,0,0}
    };
    int debugging = 0;
    int arg = EOF;
    while((arg = getopt_long(argc, argv, "bhvd", opt_list, NULL)) != EOF) {
        switch (arg) {
            case 'b':
                beep(beep_frequency, beep_duration, beep_repetitions, beep_pause, beep_increment);
                exit(EXIT_SUCCESS);
            case 'h':
                print_help();
                exit(EXIT_SUCCESS);
            case 'v':
                print_version();
                exit(EXIT_SUCCESS);
            case 'd':
                debugging = 1;
                break;
            default:
                print_usage();
                exit(EXIT_FAILURE);
        }
    }

    if (debugging) {
        debug_print();
    }
    else if (!daemonize(PID_FILE, "/", 0)) {
        exit(EXIT_FAILURE);
    }

    unsigned int poll_count = 0;
    unsigned int warning_count = 0;
    while (1) {
        if ((sleep_timeout * (poll_count + 1)) % poll_timeout == 0) {
            read_acpi();
            if (debugging) {
                printf("> Reading acpi\n");
            }
        }
        if ((sleep_timeout * (warning_count + 1)) % warning_timeout == 0) {
            if (capacity_factor <= (float)warning_level / 100.0 && strcmp(bat_state, "discharging") == 0) {
                beep(beep_frequency, beep_duration, beep_repetitions, beep_pause, beep_increment);
            }
            if (debugging) {
                printf("> Capacity: %.2f%%\tState: %s\n", capacity_factor * 100, bat_state);
            }
        }

        poll_count++;
        poll_count = poll_count % (poll_timeout / sleep_timeout);
        warning_count++;
        warning_count = warning_count % (warning_timeout / sleep_timeout);

        SLEEP(sleep_timeout);
    }

    cleanup();

    return EXIT_SUCCESS;
}