yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
26ba25
/*
26ba25
 * udev-kvm-check.c
26ba25
 *
26ba25
 * Copyright 2018 Red Hat, Inc.
26ba25
 *
26ba25
 * This is free software; you can redistribute it and/or modify it
26ba25
 * under the terms of the GNU General Public License as published by
26ba25
 * the Free Software Foundation; either version 2 of the License, or
26ba25
 * (at your option) any later version.
26ba25
 *
26ba25
 * This program is distributed in the hope that it will be useful, but
26ba25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
26ba25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26ba25
 * General Public License for more details.
26ba25
 *
26ba25
 * You should have received a copy of the GNU General Public License
26ba25
 * along with this program; if not, write to the Free Software Foundation,
26ba25
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26ba25
 *
26ba25
 */
26ba25
26ba25
#include <ctype.h>
26ba25
#include <syslog.h>
26ba25
#include <stdio.h>
26ba25
#include <stdlib.h>
26ba25
#include <string.h>
26ba25
26ba25
#define DEFAULT 0
26ba25
#define FACILITY "kvm"
26ba25
#define SYSCONFIG_KVM "/etc/sysconfig/kvm"
26ba25
26ba25
#define COUNT_MSG \
26ba25
    "%d %s now active"
26ba25
26ba25
#define SUBSCRIPTION_MSG \
26ba25
    "%d %s now active; your Red Hat Enterprise Linux subscription" \
26ba25
    " limit is %d guests. Please review your Red Hat Enterprise Linux" \
26ba25
    " subscription agreement or contact your Red Hat" \
26ba25
    " support representative for more information. You" \
26ba25
    " may review the Red Hat Enterprise subscription" \
26ba25
    " limits at http://www.redhat.com/rhel-virt-limits"
26ba25
26ba25
int get_threshold_from_file(FILE *fp)
26ba25
{
26ba25
    static const char key[] = "THRESHOLD=";
26ba25
    int pos = 0;
26ba25
    int thres;
26ba25
    int ch;
26ba25
26ba25
start:
26ba25
    /* State START - at beginning of line, search for beginning of "THRESHOLD="
26ba25
     * string.
26ba25
     */
26ba25
    ch = getc(fp);
26ba25
    if (ch == EOF) {
26ba25
        return DEFAULT;
26ba25
    }
26ba25
    if (isspace(ch)) {
26ba25
        goto start;
26ba25
    }
26ba25
    if (ch == 'T') {
26ba25
        pos = 1;
26ba25
        goto key;
26ba25
    }
26ba25
    goto eol;
26ba25
26ba25
eol:
26ba25
    /* State EOL - loop until end of line */
26ba25
    ch = getc(fp);
26ba25
    if (ch == EOF) {
26ba25
        return DEFAULT;
26ba25
    }
26ba25
    if (ch == '\n') {
26ba25
        goto start;
26ba25
    }
26ba25
    goto eol;
26ba25
26ba25
key:
26ba25
    /* State KEY - match "THRESHOLD=" string, go to THRESHOLD if found */
26ba25
    ch = getc(fp);
26ba25
    if (ch == EOF) {
26ba25
        return DEFAULT;
26ba25
    }
26ba25
    if (ch == key[pos]) {
26ba25
        pos++;
26ba25
        if (key[pos] == 0) {
26ba25
            goto threshold;
26ba25
        } else {
26ba25
            goto key;
26ba25
        }
26ba25
    }
26ba25
    goto eol;
26ba25
26ba25
threshold:
26ba25
    /* State THRESHOLD - parse number using fscanf, expect comment or space
26ba25
     * or EOL.
26ba25
     */
26ba25
    ch = getc(fp);
26ba25
    if (ch == EOF) {
26ba25
        return DEFAULT;
26ba25
    }
26ba25
    if (!isdigit(ch)) {
26ba25
        goto eol;
26ba25
    }
26ba25
    ungetc(ch, fp);
26ba25
    if (fscanf(fp, "%d", &thres) != 1) {
26ba25
        return DEFAULT;
26ba25
    }
26ba25
    ch = getc(fp);
26ba25
    if (ch == '#' || ch == EOF || ch == '\n' || isspace(ch)) {
26ba25
        return thres;
26ba25
    }
26ba25
    goto eol;
26ba25
}
26ba25
26ba25
int get_threshold()
26ba25
{
26ba25
    FILE *fp = fopen(SYSCONFIG_KVM, "r");
26ba25
    int val;
26ba25
26ba25
    if (!fp) {
26ba25
        return DEFAULT;
26ba25
    }
26ba25
26ba25
    val = get_threshold_from_file(fp);
26ba25
    fclose (fp);
26ba25
    return val;
26ba25
}
26ba25
26ba25
const char *guest(int count)
26ba25
{
26ba25
    return (count == 1 ? "guest" : "guests");
26ba25
}
26ba25
26ba25
void emit_count_message(int count)
26ba25
{
26ba25
    openlog(FACILITY, LOG_CONS, LOG_USER);
26ba25
    syslog(LOG_INFO, COUNT_MSG, count, guest(count));
26ba25
    closelog();
26ba25
}
26ba25
26ba25
void emit_subscription_message(int count, int threshold)
26ba25
{
26ba25
    openlog(FACILITY, LOG_CONS, LOG_USER);
26ba25
    syslog(LOG_WARNING, SUBSCRIPTION_MSG, count, guest(count), threshold);
26ba25
    closelog();
26ba25
}
26ba25
26ba25
int main(int argc, char **argv)
26ba25
{
26ba25
    int count, threshold;
26ba25
26ba25
    if (argc < 3)
26ba25
        exit(1);
26ba25
26ba25
    count = atoi(argv[1]);
26ba25
    threshold = get_threshold();
26ba25
26ba25
    if (!strcmp(argv[2], "create")) {
26ba25
        if (threshold == 0) {
26ba25
            emit_count_message(count);
26ba25
        } else if (count > threshold) {
26ba25
            emit_subscription_message(count, threshold);
26ba25
        }
26ba25
    } else {
26ba25
        if (count >= threshold) {
26ba25
            emit_count_message(count);
26ba25
        }
26ba25
    }
26ba25
26ba25
    return 0;
26ba25
}