Выполнение команд с помощью Обработчика управления устройством
Материал из BiTel WiKi
Amir (Обсуждение | вклад) |
Amir (Обсуждение | вклад) |
||
| Строка 49: | Строка 49: | ||
return "telnet:google.ru 80"; | return "telnet:google.ru 80"; | ||
} | } | ||
| + | |||
| + | Пример: | ||
| + | |||
| + | <source lang="java"> | ||
| + | package ru.provider.bgbilling.modules.inet.dyn.device; | ||
| + | |||
| + | import ru.bitel.bgbilling.server.util.Setup; | ||
| + | import ru.bitel.common.ParameterMap; | ||
| + | import ru.bitel.oss.systems.inventory.resource.common.bean.Device; | ||
| + | import ru.bitel.oss.systems.inventory.resource.common.bean.DeviceType; | ||
| + | import ru.bitel.oss.systems.inventory.resource.server.DeviceManagerMethod; | ||
| + | |||
| + | public class SnmpDeviceManager | ||
| + | extends ru.bitel.bgbilling.modules.inet.dyn.device.snmp.SnmpDeviceManager | ||
| + | { | ||
| + | protected ParameterMap deviceConfig; | ||
| + | |||
| + | @Override | ||
| + | public Object init( Setup setup, int moduleId, Device<?, ?> device, DeviceType deviceType, ParameterMap deviceConfig ) | ||
| + | { | ||
| + | super.init( setup, moduleId, device, deviceType, deviceConfig ); | ||
| + | |||
| + | this.deviceConfig = deviceConfig; | ||
| + | |||
| + | return null; | ||
| + | } | ||
| + | |||
| + | @DeviceManagerMethod(title = "Монитор") | ||
| + | public Object monitor() | ||
| + | { | ||
| + | return "browse:" + deviceConfig.get( "monitor.url", "http://zabbix.intranet.provider.ru/latest.php?hostid=$monitorHostId" ).replaceAll( "\\$monitorHostId", deviceConfig.get( "monitor.hostId", "" ) ); | ||
| + | } | ||
| + | |||
| + | @DeviceManagerMethod(title = "Telnet") | ||
| + | public Object telnet() | ||
| + | { | ||
| + | return "telnet:" + deviceConfig.get( "telnet.host", this.host ) + " " + deviceConfig.getInt( "telnet.port", 23 ); | ||
| + | } | ||
| + | } | ||
| + | </source> | ||
| + | Без SNMP/uptime: | ||
| + | <source lang="java"> | ||
| + | package ru.provider.bgbilling.modules.inet.dyn.device; | ||
| + | |||
| + | import java.util.List; | ||
| + | |||
| + | import ru.bitel.bgbilling.server.util.Setup; | ||
| + | import ru.bitel.common.ParameterMap; | ||
| + | import ru.bitel.oss.systems.inventory.resource.common.bean.Device; | ||
| + | import ru.bitel.oss.systems.inventory.resource.common.bean.DeviceType; | ||
| + | import ru.bitel.oss.systems.inventory.resource.server.DeviceManagerAdapter; | ||
| + | import ru.bitel.oss.systems.inventory.resource.server.DeviceManagerMethod; | ||
| + | |||
| + | public class DeviceManager | ||
| + | extends DeviceManagerAdapter | ||
| + | implements ru.bitel.oss.systems.inventory.resource.server.DeviceManager | ||
| + | { | ||
| + | protected ParameterMap deviceConfig; | ||
| + | |||
| + | protected String host; | ||
| + | |||
| + | @Override | ||
| + | public Object init( Setup setup, int moduleId, Device<?, ?> device, DeviceType deviceType, ParameterMap deviceConfig ) | ||
| + | throws Exception | ||
| + | { | ||
| + | super.init( setup, moduleId, device, deviceType, deviceConfig ); | ||
| + | |||
| + | this.deviceConfig = deviceConfig; | ||
| + | |||
| + | final List<String[]> hosts = device.getHostsAsString(); | ||
| + | final String[] host = (hosts != null && hosts.size() > 0) ? hosts.get( 0 ) : null; | ||
| + | |||
| + | this.host = deviceConfig.get( "snmp.host", host != null ? host[0] : device.getHost() ); | ||
| + | |||
| + | return null; | ||
| + | } | ||
| + | |||
| + | @DeviceManagerMethod(title = "Монитор") | ||
| + | public Object monitor() | ||
| + | { | ||
| + | return "browse:" + deviceConfig.get( "monitor.url", "http://zabbix.intranet.provider.ru/latest.php?hostid=$monitorHostId" ).replaceAll( "\\$monitorHostId", deviceConfig.get( "monitor.hostId", "" ) ); | ||
| + | } | ||
| + | |||
| + | @DeviceManagerMethod(title = "Telnet") | ||
| + | public Object telnet() | ||
| + | { | ||
| + | return "telnet:" + deviceConfig.get( "telnet.host", this.host ) + " " + deviceConfig.getInt( "telnet.port", 23 ); | ||
| + | } | ||
| + | } | ||
| + | </source> | ||
Версия 14:10, 12 сентября 2014
В контекстном меню дерева устройств доступен пункт выполнить команду. Данный пункт вызывает метод у Обработчика управления устройством (DeviceManager), указанном в типе устройства. Таким методом, например, является uptime() из ru.bitel.bgbilling.modules.inet.dyn.device.snmp.SnmpDeviceManager, хотя нужен он не для ручного вызова из контекстного меню.
@Override
public Object uptime()
throws Exception
{
return snmpClient.get( uptimeOid, -1, Long.class );
}
Команды-методы можно именовать с помощью аннотации ru.bitel.oss.systems.inventory.resource.server.DeviceManagerMethod, в этом случае они будут сразу доступны в контекстном меню:
import ru.bitel.oss.systems.inventory.resource.server.DeviceManagerMethod;
@DeviceManagerMethod(title = "Перезагрузить")
public Object reboot()
throws Exception
{
return return snmpClient.set( new AsnObjectId( "1.2.3.4.5.6.7.8.9" ).getOid(), -1, Long.class );
}
После выполнения можно вернуть строку определенного вида, чтобы клиент биллинга попытался открыть браузер с указанным URL:
@DeviceManagerMethod(title = "Статус")
public Object status()
throws Exception
{
return "browse:http://google.ru";
}
Или telnet:
@DeviceManagerMethod(title = "Telnet")
public Object telnet()
throws Exception
{
return "telnet:google.ru 80";
}
Как аргумент в методе можно указать ru.bitel.bgbilling.modules.inet.access.manage.event.InetDeviceManageEvent, чтобы узнать, например, userId пользователя, выполняющего команду:
import ru.bitel.bgbilling.modules.inet.access.manage.event.InetDeviceManageEvent;
@DeviceManagerMethod(title = "Telnet")
public Object telnet2( InetDeviceManageEvent e )
throws Exception
{
logger.info( e.getUserId() );
return "telnet:google.ru 80";
}
Пример:
package ru.provider.bgbilling.modules.inet.dyn.device; import ru.bitel.bgbilling.server.util.Setup; import ru.bitel.common.ParameterMap; import ru.bitel.oss.systems.inventory.resource.common.bean.Device; import ru.bitel.oss.systems.inventory.resource.common.bean.DeviceType; import ru.bitel.oss.systems.inventory.resource.server.DeviceManagerMethod; public class SnmpDeviceManager extends ru.bitel.bgbilling.modules.inet.dyn.device.snmp.SnmpDeviceManager { protected ParameterMap deviceConfig; @Override public Object init( Setup setup, int moduleId, Device<?, ?> device, DeviceType deviceType, ParameterMap deviceConfig ) { super.init( setup, moduleId, device, deviceType, deviceConfig ); this.deviceConfig = deviceConfig; return null; } @DeviceManagerMethod(title = "Монитор") public Object monitor() { return "browse:" + deviceConfig.get( "monitor.url", "http://zabbix.intranet.provider.ru/latest.php?hostid=$monitorHostId" ).replaceAll( "\\$monitorHostId", deviceConfig.get( "monitor.hostId", "" ) ); } @DeviceManagerMethod(title = "Telnet") public Object telnet() { return "telnet:" + deviceConfig.get( "telnet.host", this.host ) + " " + deviceConfig.getInt( "telnet.port", 23 ); } }
Без SNMP/uptime:
package ru.provider.bgbilling.modules.inet.dyn.device; import java.util.List; import ru.bitel.bgbilling.server.util.Setup; import ru.bitel.common.ParameterMap; import ru.bitel.oss.systems.inventory.resource.common.bean.Device; import ru.bitel.oss.systems.inventory.resource.common.bean.DeviceType; import ru.bitel.oss.systems.inventory.resource.server.DeviceManagerAdapter; import ru.bitel.oss.systems.inventory.resource.server.DeviceManagerMethod; public class DeviceManager extends DeviceManagerAdapter implements ru.bitel.oss.systems.inventory.resource.server.DeviceManager { protected ParameterMap deviceConfig; protected String host; @Override public Object init( Setup setup, int moduleId, Device<?, ?> device, DeviceType deviceType, ParameterMap deviceConfig ) throws Exception { super.init( setup, moduleId, device, deviceType, deviceConfig ); this.deviceConfig = deviceConfig; final List<String[]> hosts = device.getHostsAsString(); final String[] host = (hosts != null && hosts.size() > 0) ? hosts.get( 0 ) : null; this.host = deviceConfig.get( "snmp.host", host != null ? host[0] : device.getHost() ); return null; } @DeviceManagerMethod(title = "Монитор") public Object monitor() { return "browse:" + deviceConfig.get( "monitor.url", "http://zabbix.intranet.provider.ru/latest.php?hostid=$monitorHostId" ).replaceAll( "\\$monitorHostId", deviceConfig.get( "monitor.hostId", "" ) ); } @DeviceManagerMethod(title = "Telnet") public Object telnet() { return "telnet:" + deviceConfig.get( "telnet.host", this.host ) + " " + deviceConfig.getInt( "telnet.port", 23 ); } }
