aboutsummaryrefslogtreecommitdiffstats
path: root/src/udev-kvm-check.c
blob: 55595ee5ae5acd33048fe9cac4b3c22ec72102f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <ctype.h>
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shvar.h"

#define DEFAULT 0
#define FACILITY "kvm"
#define SYSCONFIG_KVM "/etc/sysconfig/kvm"

#define COUNT_MSG \
    "%d %s now active"

#define SUBSCRIPTION_MSG \
    "%d %s now active; your Red Hat Enterprise Linux subscription" \
    " limit is %d guests. Please review your Red Hat Enterprise Linux" \
    " subscription agreement or contact your Red Hat" \
    " support representative for more information. You" \
    " may review the Red Hat Enterprise subscription" \
    " limits at http://www.redhat.com/rhel-virt-limits"


int get_threshold()
{
    int val = 0;
    char *cval = NULL;

    shvarFile *file = svNewFile(SYSCONFIG_KVM);
    if (!file)
        return 0;

    cval = svGetValue(file, "THRESHOLD");

    svCloseFile(file);

    if (cval == NULL)
        return 0;

    val = atoi(cval);

    free(cval);
    
    return val;
}

const char *guest(int count)
{
    return (count == 1 ? "guest" : "guests");
}

void emit_count_message(int count)
{
    openlog(FACILITY, LOG_CONS, LOG_USER);
    syslog(LOG_INFO, COUNT_MSG, count, guest(count));
    closelog();
}

void emit_subscription_message(int count, int threshold)
{
    openlog(FACILITY, LOG_CONS, LOG_USER);
    syslog(LOG_WARNING, SUBSCRIPTION_MSG, count, guest(count), threshold);
    closelog();
}

int main(int argc, char **argv)
{
    int count, threshold;

    if (argc < 3)
        exit(1);

    count = atoi(argv[1]);
    threshold = get_threshold();

    if (!strcmp(argv[2], "create")) {
        if (threshold == 0) {
            emit_count_message(count);
        } else if (count > threshold) {
            emit_subscription_message(count, threshold);
        }
    } else {
        if (count >= threshold) {
            emit_count_message(count);
        }
    }

    return 0;
}