from django.utils.translation import ugettext as _
from freenasUI.system.alert import alertPlugins, Alert, BaseAlert

from subprocess import Popen, PIPE


class USBStorageAlert(BaseAlert):

    interval = 5

    def run(self):
        proc = Popen('camcontrol devlist -v | grep -m1 -A1 umass', stdout=PIPE, stderr=PIPE, shell=True)
        usbdevname = proc.communicate()
        if proc.returncode == 0:

            usbdevname = str(usbdevname[0])
            usbdevname = usbdevname[usbdevname.find('<')+1:usbdevname.find('>')]

            return [Alert(
                Alert.CRIT,
                _('A USB storage device named "' + usbdevname + '" has been '
                'connected to this system. Please remove that USB device to '
                'prevent problems with system boot or HA failover.'),
                hardware=True,
            )]


alertPlugins.register(USBStorageAlert)
