This file is indexed.

/usr/share/pyshared/archipelagentvirtualmachineoomkiller/oomkiller.py is in archipel-agent-virtualmachine-oomkiller 0.6.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# -*- coding: utf-8 -*-
#
# oomkiller.py
#
# Copyright (C) 2010 Antoine Mercadal <antoine.mercadal@inframonde.eu>
# This file is part of ArchipelProject
# http://archipelproject.org
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import commands
import sqlite3
import xmpp

from archipelcore.archipelPlugin import TNArchipelPlugin
from archipelcore.utils import build_error_iq


ARCHIPEL_NS_OOM_KILLER = "archipel:vm:oom"


class TNOOMKiller (TNArchipelPlugin):

    def __init__(self, configuration, entity, entry_point_group):
        """
        Initialize the plugin.
        @type configuration: Configuration object
        @param configuration: the configuration
        @type entity: L{TNArchipelEntity}
        @param entity: the entity that owns the plugin
        @type entry_point_group: string
        @param entry_point_group: the group name of plugin entry_point
        """
        TNArchipelPlugin.__init__(self, configuration=configuration, entity=entity, entry_point_group=entry_point_group)
        self.entity.register_hook("HOOK_VM_INITIALIZE", method=self.vm_initialized)
        self.entity.register_hook("HOOK_VM_CREATE", method=self.vm_create)
        self.entity.register_hook("HOOK_VM_TERMINATE", method=self.vm_terminate)
        self.database = sqlite3.connect(self.configuration.get("OOMKILLER", "database"), check_same_thread=False)
        self.database.execute("create table if not exists oomkiller (uuid text unique, adjust int)")
        self.database.commit()
        self.entity.log.info("OOM: module oom killer initialized")
        # permissions
        self.entity.permission_center.create_permission("oom_getadjust", "Authorizes user to get OOM values", False)
        self.entity.permission_center.create_permission("oom_setadjust", "Authorizes user to set OOM values", False)

    ### Module implementation

    def register_handlers(self):
        """
        This method will be called by the plugin user when it will be
        necessary to register module for listening to stanza.
        """
        self.entity.xmppclient.RegisterHandler('iq', self.process_iq, ns=ARCHIPEL_NS_OOM_KILLER)

    def unregister_handlers(self):
        """
        Unregister the handlers.
        """
        self.entity.xmppclient.UnregisterHandler('iq', self.process_iq, ns=ARCHIPEL_NS_OOM_KILLER)

    @staticmethod
    def plugin_info():
        """
        Return informations about the plugin.
        @rtype: dict
        @return: dictionary contaning plugin informations
        """
        plugin_friendly_name           = "Virtual Machine OOM Killer"
        plugin_identifier              = "oomkiller"
        plugin_configuration_section   = "OOMKILLER"
        plugin_configuration_tokens    = ["database"]
        return {    "common-name"               : plugin_friendly_name,
                    "identifier"                : plugin_identifier,
                    "configuration-section"     : plugin_configuration_section,
                    "configuration-tokens"      : plugin_configuration_tokens }


    ### Hooks

    def vm_create(self, origin, user_info, parameters):
        """
        Handle create HOOK_VM_CREATE.
        @type origin: L{TNArchipelEntity}
        @param origin: the origin of the hook
        @type user_info: object
        @param user_info: random user info
        @type parameters: object
        @param parameters: runtim argument
        """
        oom_info = self.get_oom_info()
        self.entity.log.info("OOM: value retrieved %s" % str(oom_info))
        self.set_oom_info(oom_info["adjust"], oom_info["score"])
        self.entity.log.info("OOM: value for vm with uuid %s have been restored." % self.entity.uuid)

    def vm_terminate(self, origin, user_info, parameters):
        """
        Handle create HOOK_VM_TERMINATE.
        @type origin: L{TNArchipelEntity}
        @param origin: the origin of the hook
        @type user_info: object
        @param user_info: random user info
        @type parameters: object
        @param parameters: runtim argument
        """
        self.database.execute("DELETE FROM oomkiller WHERE uuid=?", (self.entity.uuid, ))
        self.database.commit()
        self.database.close()
        self.entity.log.info("OOM: information for vm with uuid %s has been removed." % self.entity.uuid)

    def vm_initialized(self, origin, user_info, parameters):
        """
        Handle create HOOK_VM_INITIALIZE.
        @type origin: L{TNArchipelEntity}
        @param origin: the origin of the hook
        @type user_info: object
        @param user_info: random user info
        @type parameters: object
        @param parameters: runtim argument
        """
        oom_info = self.get_oom_info()
        self.set_oom_info(oom_info["adjust"], oom_info["score"])
        self.entity.log.info("OOM: information for vm with uuid %s have been removed." % self.entity.uuid)


    ### OOM information management

    def get_oom_info(self):
        """
        Get the OOM info from database.
        @rtype: dict
        @return: dict contaning OOM status
        """
        adj_value = 0
        score_value = 0
        rows = self.database.execute("SELECT adjust FROM oomkiller WHERE uuid=?", (self.entity.uuid, ))
        for values in rows:
            adj_value = values[0]
            score_value = 0
        return {"adjust": adj_value, "score": score_value}

    def set_oom_info(self, adjust, score):
        """
        Set the OOM info both on file if exists and on database.
        @type adjust: int
        @param adjust: the value of adjust
        @type score: int
        @param score: the value of the score
        """
        try:
            pid = int(commands.getoutput("ps -ef | grep kvm | grep %s | grep -v grep" % self.entity.uuid).split()[1])
            f = open("/proc/%d/oom_adj" % pid, "w")
            f.write(str(adjust))
            f.close()
        except Exception as ex:
            self.entity.log.warning("OOM: No valid PID. storing value only on database: " + str(ex))
        try:
            self.database.execute("INSERT INTO oomkiller VALUES (?, ?)", (self.entity.uuid, int(adjust)))
        except:
            self.database.execute("UPDATE oomkiller SET adjust=? WHERE uuid=?", (int(adjust), self.entity.uuid))
        try:
            self.database.commit()
        except Exception as ex:
            self.entity.log.warning("OOM: Unable to commit change in DB while setting OOM: " + str(ex))



    ### XMPP handlers

    def process_iq(self, conn, iq):
        """
        This method is invoked when a ARCHIPEL_NS_OOM_KILLER IQ is received.
        It understands IQ of type:
            - do-something
        @type conn: xmpp.Dispatcher
        @param conn: ths instance of the current connection that send the stanza
        @type iq: xmpp.Protocol.Iq
        @param iq: the received IQ
        """
        reply = None
        action = self.entity.check_acp(conn, iq)
        self.entity.check_perm(conn, iq, action, -1, prefix="oom_")
        if action == "getadjust":
            reply = self.iq_oom_get_adjust(iq)
        elif action == "setadjust":
            reply = self.iq_oom_set_adjust(iq)
        if reply:
            conn.send(reply)
            raise xmpp.protocol.NodeProcessed

    def iq_oom_get_adjust(self, iq):
        """
        Return the value of the oom_adjust of the virtual machine.
        @type iq: xmpp.Protocol.Iq
        @param iq: the received IQ
        @rtype: xmpp.Protocol.Iq
        @return: a ready to send IQ containing the result of the action
        """
        try:
            reply = iq.buildReply("result")
            oom_info = self.get_oom_info()
            adj_node = xmpp.Node(tag="oom", attrs={"adjust": oom_info["adjust"], "score": oom_info["score"]})
            reply.setQueryPayload([adj_node])
        except Exception as ex:
            reply = build_error_iq(self, ex, iq)
        return reply


    def iq_oom_set_adjust(self, iq):
        """
        Set the adjust value of oom killer from -16:15 plus special -17 value that disable oom killer for the process
        the lower the value his the higher the likelihood of killing the process.
        @type iq: xmpp.Protocol.Iq
        @param iq: the received IQ
        @rtype: xmpp.Protocol.Iq
        @return: a ready to send IQ containing the result of the action
        """
        try:
            reply = iq.buildReply("result")
            value = iq.getTag("query").getTag("archipel").getAttr("adjust")
            self.set_oom_info(value, 0)
            self.entity.push_change("oom", "adjusted")
        except Exception as ex:
            reply = build_error_iq(self, ex, iq)
        return reply