001/* ***** BEGIN LICENSE BLOCK ***** 002 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 003 * 004 * The contents of this file are subject to the Mozilla Public License Version 005 * 1.1 (the "License"); you may not use this file except in compliance with 006 * the License. You may obtain a copy of the License at 007 * http://www.mozilla.org/MPL/ 008 * 009 * Software distributed under the License is distributed on an "AS IS" basis, 010 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 011 * for the specific language governing rights and limitations under the 012 * License. 013 * 014 * The Original Code is part of dcm4che, an implementation of DICOM(TM) in 015 * Java(TM), hosted at https://github.com/gunterze/dcm4che. 016 * 017 * The Initial Developer of the Original Code is 018 * Agfa Healthcare. 019 * Portions created by the Initial Developer are Copyright (C) 2012 020 * the Initial Developer. All Rights Reserved. 021 * 022 * Contributor(s): 023 * See @authors listed below 024 * 025 * Alternatively, the contents of this file may be used under the terms of 026 * either the GNU General Public License Version 2 or later (the "GPL"), or 027 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 028 * in which case the provisions of the GPL or the LGPL are applicable instead 029 * of those above. If you wish to allow use of your version of this file only 030 * under the terms of either the GPL or the LGPL, and not to allow others to 031 * use your version of this file under the terms of the MPL, indicate your 032 * decision by deleting the provisions above and replace them with the notice 033 * and other provisions required by the GPL or the LGPL. If you do not delete 034 * the provisions above, a recipient may use your version of this file under 035 * the terms of any one of the MPL, the GPL or the LGPL. 036 * 037 * ***** END LICENSE BLOCK ***** */ 038 039package org.dcm4che3.tool.syslog; 040 041import java.io.ByteArrayOutputStream; 042import java.io.File; 043import java.io.FileDescriptor; 044import java.io.FileInputStream; 045import java.io.IOException; 046import java.util.List; 047import java.util.ResourceBundle; 048import java.util.concurrent.Executors; 049import java.util.concurrent.ScheduledExecutorService; 050 051import org.apache.commons.cli.CommandLine; 052import org.apache.commons.cli.MissingOptionException; 053import org.apache.commons.cli.OptionBuilder; 054import org.apache.commons.cli.Options; 055import org.apache.commons.cli.ParseException; 056import org.dcm4che3.net.Connection; 057import org.dcm4che3.net.Connection.Protocol; 058import org.dcm4che3.net.Device; 059import org.dcm4che3.net.audit.AuditLogger; 060import org.dcm4che3.net.audit.AuditRecordRepository; 061import org.dcm4che3.tool.common.CLIUtils; 062import org.dcm4che3.util.SafeClose; 063import org.dcm4che3.util.StreamUtils; 064import org.dcm4che3.util.StringUtils; 065 066/** 067 * @author Gunter Zeilinger <gunterze@gmail.com> 068 * 069 */ 070public class Syslog { 071 072 private static ResourceBundle rb = 073 ResourceBundle.getBundle("org.dcm4che3.tool.syslog.messages"); 074 075 private final Connection conn = new Connection(); 076 private final Connection remote = new Connection(); 077 private final AuditLogger auditLogger = new AuditLogger(); 078 private final AuditRecordRepository arr = new AuditRecordRepository(); 079 private final Device logDevice = new Device("syslog"); 080 private final Device arrDevice = new Device("syslogd"); 081 private int delayBetweenMessages; 082 083 084 public Syslog() throws IOException { 085 logDevice.addDeviceExtension(auditLogger); 086 logDevice.addConnection(conn); 087 arrDevice.addDeviceExtension(arr); 088 arrDevice.addConnection(remote); 089 auditLogger.addAuditRecordRepositoryDevice(arrDevice); 090 } 091 092 private void setProtocol(Connection.Protocol protocol) { 093 conn.setProtocol(protocol); 094 remote.setProtocol(protocol); 095 auditLogger.addConnection(conn); 096 arr.addConnection(remote); 097 } 098 099 private static CommandLine parseComandLine(String[] args) 100 throws ParseException{ 101 Options opts = new Options(); 102 addConnectOption(opts); 103 addBindOption(opts); 104 addAuditLogger(opts); 105 addSendOptions(opts); 106 CLIUtils.addSocketOptions(opts); 107 CLIUtils.addTLSOptions(opts); 108 CLIUtils.addCommonOptions(opts); 109 return CLIUtils.parseComandLine(args, opts, rb, Syslog.class); 110 } 111 112 @SuppressWarnings("static-access") 113 private static void addConnectOption(Options opts) { 114 opts.addOption(OptionBuilder 115 .hasArg() 116 .withArgName("host:port") 117 .withDescription(rb.getString("connect")) 118 .withLongOpt("connect") 119 .create("c")); 120 opts.addOption(OptionBuilder 121 .hasArg() 122 .withArgName("[user:password@]host:port") 123 .withDescription(rb.getString("proxy")) 124 .withLongOpt("proxy") 125 .create(null)); 126 opts.addOption(null, "udp", false, rb.getString("udp")); 127 CLIUtils.addConnectTimeoutOption(opts); 128 opts.addOption(OptionBuilder 129 .hasArg() 130 .withArgName("ms") 131 .withDescription(rb.getString("idle-timeout")) 132 .withLongOpt("idle-timeout") 133 .create(null)); 134 } 135 136 @SuppressWarnings("static-access") 137 private static void addBindOption(Options opts) { 138 opts.addOption(OptionBuilder 139 .hasArg() 140 .withArgName("ip") 141 .withDescription(rb.getString("bind")) 142 .withLongOpt("bind") 143 .create("b")); 144 } 145 146 @SuppressWarnings("static-access") 147 private static void addAuditLogger(Options opts) { 148 opts.addOption(OptionBuilder 149 .hasArg() 150 .withArgName("facility") 151 .withDescription(rb.getString("facility")) 152 .withLongOpt("facility") 153 .create(null)); 154 opts.addOption(OptionBuilder 155 .hasArg() 156 .withArgName("level") 157 .withDescription(rb.getString("level")) 158 .withLongOpt("level") 159 .create(null)); 160 opts.addOption(OptionBuilder 161 .hasArg() 162 .withArgName("name") 163 .withDescription(rb.getString("app-name")) 164 .withLongOpt("app-name") 165 .create(null)); 166 opts.addOption(OptionBuilder 167 .hasArg() 168 .withArgName("id") 169 .withDescription(rb.getString("msg-id")) 170 .withLongOpt("msg-id") 171 .create(null)); 172 opts.addOption(null, "utc", false, rb.getString("utc")); 173 opts.addOption(null, "no-bom", false, rb.getString("no-bom")); 174 opts.addOption(OptionBuilder 175 .hasArg() 176 .withArgName("s") 177 .withDescription(rb.getString("retry")) 178 .withLongOpt("retry") 179 .create(null)); 180 opts.addOption(OptionBuilder 181 .hasArg() 182 .withArgName("dir") 183 .withDescription(rb.getString("spool-dir")) 184 .withLongOpt("spool-dir") 185 .create(null)); 186 } 187 188 @SuppressWarnings("static-access") 189 private static void addSendOptions(Options opts) { 190 opts.addOption(OptionBuilder 191 .hasArg() 192 .withArgName("ms") 193 .withDescription(rb.getString("delay")) 194 .withLongOpt("delay") 195 .create(null)); 196 } 197 198 private static void configureConnect(Connection conn, CommandLine cl) 199 throws MissingOptionException, ParseException { 200 if (!cl.hasOption("c")) 201 throw new MissingOptionException( 202 CLIUtils.rb.getString("missing-connect-opt")); 203 204 String[] hostPort = StringUtils.split(cl.getOptionValue("c"), ':'); 205 if (hostPort.length != 2) 206 throw new ParseException(CLIUtils.rb.getString("invalid-connect-opt")); 207 208 conn.setHostname(hostPort[0]); 209 conn.setPort(Integer.parseInt(hostPort[1])); 210 211 conn.setHttpProxy(cl.getOptionValue("proxy")); 212 } 213 214 private static void configureBind(Connection conn, CommandLine cl) { 215 if (cl.hasOption("b")) 216 conn.setHostname(cl.getOptionValue("b")); 217 } 218 219 @SuppressWarnings("unchecked") 220 public static void main(String[] args) { 221 try { 222 CommandLine cl = parseComandLine(args); 223 Syslog main = new Syslog(); 224 configureConnect(main.remote, cl); 225 main.setProtocol(toProtocol(cl)); 226 configureAuditLogger(main.auditLogger, cl); 227 main.setDelayBetweenMessages( 228 CLIUtils.getIntOption(cl, "delay", 0)); 229 configureBind(main.conn, cl); 230 CLIUtils.configure(main.conn, cl); 231 try { 232 main.init(); 233 main.sendFiles(cl.getArgList()); 234 main.waitForNoQueuedMessages(); 235 } finally { 236 main.close(); 237 } 238 } catch (ParseException e) { 239 System.err.println("syslog: " + e.getMessage()); 240 System.err.println(rb.getString("try")); 241 System.exit(2); 242 } catch (Exception e) { 243 System.err.println("syslog: " + e.getMessage()); 244 e.printStackTrace(); 245 System.exit(2); 246 } 247 } 248 249 private void waitForNoQueuedMessages() throws InterruptedException { 250 auditLogger.waitForNoQueuedMessages(0); 251 } 252 253 private void setDelayBetweenMessages(int delayBetweenMessages) { 254 this.delayBetweenMessages = delayBetweenMessages; 255 } 256 257 private static void configureAuditLogger(AuditLogger logger, CommandLine cl) { 258 logger.setFacility(toFacility(cl)); 259 logger.setSuccessSeverity(toSeverity(cl)); 260 logger.setApplicationName(cl.getOptionValue("app-name")); 261 logger.setMessageID(cl.getOptionValue("msg-id", AuditLogger.MESSAGE_ID)); 262 logger.setIncludeBOM(!cl.hasOption("no-bom")); 263 logger.setTimestampInUTC(cl.hasOption("utc")); 264 if (cl.hasOption("spool-dir")) 265 logger.setSpoolDirectory(new File(cl.getOptionValue("spool-dir"))); 266 logger.setRetryInterval(CLIUtils.getIntOption(cl, "retry", 0)); 267 } 268 269 private static AuditLogger.Severity toSeverity(CommandLine cl) { 270 return AuditLogger.Severity.valueOf( 271 cl.getOptionValue("level", "info")); 272 } 273 274 private static AuditLogger.Facility toFacility(CommandLine cl) { 275 return AuditLogger.Facility.valueOf( 276 cl.getOptionValue("facility", "authpriv")); 277 } 278 279 private static Protocol toProtocol(CommandLine cl) { 280 return cl.hasOption("udp") 281 ? Connection.Protocol.SYSLOG_UDP 282 : Connection.Protocol.SYSLOG_TLS; 283 } 284 285 public void init() { 286 remote.setTlsProtocols(conn.tlsProtocols()); 287 remote.setTlsCipherSuites(conn.getTlsCipherSuites()); 288 logDevice.setScheduledExecutor(Executors.newSingleThreadScheduledExecutor()); 289 auditLogger.sendQueuedMessages(); 290 } 291 292 public void close() { 293 auditLogger.closeActiveConnection(); 294 ScheduledExecutorService scheduler = logDevice.getScheduledExecutor(); 295 if (scheduler != null) 296 scheduler.shutdown(); 297 } 298 299 public void sendFiles(List<String> pathnames) throws Exception { 300 int count = 0; 301 for (String pathname : pathnames) { 302 if (count++ > 0 && delayBetweenMessages > 0) 303 Thread.sleep(delayBetweenMessages); 304 byte[] b = readFile(pathname); 305 auditLogger.write(auditLogger.timeStamp(), 306 auditLogger.getSuccessSeverity(), 307 b, 0, b.length); 308 } 309 } 310 311 private byte[] readFile(String pathname) throws IOException { 312 FileInputStream in = null; 313 try { 314 if (pathname.equals("-")) { 315 in = new FileInputStream(FileDescriptor.in); 316 ByteArrayOutputStream buf = new ByteArrayOutputStream(); 317 StreamUtils.copy(in, buf); 318 return buf.toByteArray(); 319 } else { 320 File f = new File(pathname); 321 in = new FileInputStream(f); 322 byte[] b = new byte[(int) f.length()]; 323 StreamUtils.readFully(in, b, 0, b.length); 324 return b; 325 } 326 } finally { 327 SafeClose.close(in); 328 } 329 } 330}