yeahuh / rpms / qemu-kvm

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