1.0.2 • Published 9 years ago

icecast-monitor v1.0.2

Weekly downloads
4
License
MIT
Repository
github
Last release
9 years ago

Quickstart

Build Status Code Climate Test Coverage

Powerful & handy interface for icecast-kh monitoring & statistics collection (admin access is required).

  • Able to collect icecast stats in realtime;
  • Provides easy access to all stats, available in web admin;
  • Can deal with very large amounts of data in memory-effective way;
  • Has only one npm dependency.

To install latest stable version use npm install icecast-monitor command.

Options

To access icecast monitor features create Monitor instance:

var Monitor = require('icecast-monitor');
var monitor = new Monitor({
  host: 'icecast.dev',
  port: 80,
  user: 'admin',
  password: 'hackme'
});

Following constructor parameters are available:

ParameterTypeRequiredDescription
hostStringYesIP or DNS name
portIntegerNoPort number (defaults to 80)
userStringYesAdmin username
passwordStringYesAdmin password

Methods

monitor.createFeed

Creates Monitor.Feed instance, which establishes persistent connection with icecast & processes its events feed.

monitor.createFeed(function(err, feed) {
  if (err) throw err;
  
  // Handle wildcard events
  feed.on('*', function(event, data, raw) {
    console.log(event, data, raw);
  });

  // Handle usual events
  feed.on('mount.listeners', function(listeners, raw) {
    console.log(listeners, raw);
  });
});

monitor.getServerInfo

Returns information about icecast server. Please see Monitor.XmlStreamParser server event data for details.

monitor.getServerInfo(function(err, server) {
  if (err) throw err;
  console.log(server);
});

monitor.getSources

Returns array with all audio sources (without detailed listeners information). Please see Monitor.XmlStreamParser source event for data provided about every source.

monitor.getSources(function(err, sources) {
  if (err) throw err;
  console.log(sources);
});

monitor.getSource

Provides detailed information about specified source & its listeners.

monitor.getSource('/some-mountpoint', function(err, source) {
  if (err) throw err;
  console.log(source);
});

Returns same data as Monitor.XmlStreamParser source event, with one difference: listeners parameter will contain array with information about every listener.

monitor.getListeners

Returns array with all listeners, connected to icecast server. Please see Monitor.XmlStreamParser listener event data for details. Can produce huge amounts of data, use wisely.

monitor.getListeners(function(err, listeners) {
  if (err) throw err;
  console.log(listeners);
});

monitor.createStatsXmlStream

Performs HTTP request to given icecast url path and returns stream for further processing. Can be useful to process large icecast XML output using Monitor.XmlStreamParser.

We use following icecast url paths:

  • /admin/stats
    • icecast server information
    • sources detailed information
    • no detailed listeners information
  • /admin/stats?mount=/$mount
    • icecast server information
    • specified source information
    • detailed information about connected listeners
  • /admin/listmounts?with_listeners
    • no information about icecast server
    • minimal information about sources
    • and detailed information about all icecast listeners
// Collect sources without storing them in a memory
monitor.createStatsXmlStream('/admin/stats', function(err, xmlStream) {
  if (err) throw err;
  
  var xmlParser = new Monitor.XmlStreamParser();
 
  xmlParser.on('error', function(err) {
    console.log('error', err); 
  });
  
  xmlParser.on('source', function(source) {
    // Do work with received source
    console.log('source', source);
  });

  // Finish event is being piped from xmlStream
  xmlParser.on('finish', function() {
    console.log('all sources are processed');
  });

  xmlStream.pipe(xmlParser);
});

Feed

Establishes persistent connection with icecast using STATS HTTP method & processes events feed in realtime. Best way to create is to use monitor.createFeed method, which injects all necessary parameters.

Events

For mount. and server. events user-callback is provided with following parameters:

ParameterTypeDescription
eventStringEvent name (present only for wildcard events)
dataMixedParsed parameter(s), is described for each event below
rawStringRaw message received from icecast

mount.audioCodecId

EVENT /test.mp3 audio_codecid 2
ParameterTypeDescription
mountStringMountpoint name
dataIntegerAudio codec id: 2 for mp3, 10 for aac

mount.audioInfo

Displays audio encoding information.

EVENT /test.mp3 audio_info channels=2;samplerate=44100;bitrate=64
ParameterTypeDescription
mountStringMountpoint name
dataObjectAudio channel info
data.channelsIntegerNumber of channels
data.sampleRateIntegerSample rate
data.bitrateIntegerBitrate (kbps)

mount.authenticator

EVENT /test.mp3 authenticator command
ParameterTypeDescription
mountStringMountpoint name
dataStringAuthenticator type

mount.bitrate

EVENT /test.mp3 bitrate 64
ParameterTypeDescription
mountStringMountpoint name
dataIntegerBitrate (kbps), used for stats & YP

mount.connected

EVENT /test.mp3 connected 180423
ParameterTypeDescription
mountStringMountpoint name
dataIntegerConnection duration in seconds

mount.delete

Emitted when mount is deleted. Allows to notify relays about deleted source immediately (rather than wait for polling by the slaves).

DELETE /test.mp3
ParameterTypeDescription
mountStringDeleted mountpoint name

mount.flush

FLUSH /test.mp3
ParameterTypeDescription
mountStringFlushed mountpoint name

mount.genre

EVENT /test.mp3 genre Misc
ParameterTypeDescription
mountStringMountpoint name
dataStringGenre name, used for stats & YP

mount.incomingBitrate

EVENT /test.mp3 incoming_bitrate 127064
ParameterTypeDescription
mountStringMountpoint name
dataIntegerSource bitrate (bps)

mount.listenerConnections

EVENT /test.mp3 listener_connections 4
ParameterTypeDescription
mountStringMountpoint name
dataIntegerConnections number

mount.listenerPeak

EVENT /test.mp3 listener_peak 2
ParameterTypeDescription
mountStringMountpoint name
dataIntegerMax detected number of simultaneous listeners

mount.listeners

EVENT /test.mp3 listeners 2
ParameterTypeDescription
mountStringMountpoint name
dataIntegerCurrent listeners number

mount.listenUrl

EVENT /11-31.mp3 listenurl http://icecast.dev:80/test.mp3
ParameterTypeDescription
mountStringMountpoint name
dataStringAudio stream url

mount.maxListeners

EVENT /11-31.mp3 max_listeners -1
ParameterTypeDescription
mountStringMountpoint name
dataIntegerSimultanious listeners limit

mount.metadataUpdated

Is emitted when track is updated.

EVENT /test.mp3 metadata_updated 06/Aug/2015:14:05:05 +0300
ParameterTypeDescription
mountStringMountpoint name
dataStringDate when metadata was updated

mount.mpegChannels

EVENT /test.mp3 mpeg_channels 2
ParameterTypeDescription
mountStringMountpoint name
dataIntegerNumber of audio channels

mount.mpegSampleRate

EVENT /test.mp3 mpeg_samplerate 44100
ParameterTypeDescription
mountStringMountpoint name
dataIntegerSample rate

mount.new

Emitted when new mount is created. Allows to notify relays about new source immediately (rather than wait for polling by the slaves).

NEW audio/mpeg /229-682.mp3
ParameterTypeDescription
mountStringMountpoint
dataStringMime type

mount.outgoingKBitrate

EVENT /test.mp3 outgoing_kbitrate 0
ParameterTypeDescription
mountStringMountpoint name
dataIntegerOutgoing bitrate (kbps)

mount.public

Displays mount visibility (advertisement) setting.

EVENT /test.mp3 public 1
ParameterTypeDescription
mountStringMountpoint name
dataIntegerPossible values: -1 (up to source client / relay) , 0 (disable), 1 (force advertisement)

mount.queueSize

EVENT /test.mp3 queue_size 65828
ParameterTypeDescription
mountStringMountpoint name
dataIntegerQueue size

mount.serverDescription

EVENT /test.mp3 server_description My station description
ParameterTypeDescription
mountStringMountpoint name
dataStringUser-defined station description

mount.serverName

EVENT /test.mp3 server_name TestFM
ParameterTypeDescription
mountStringMountpoint name
dataStringUser-defined station name

mount.serverType

EVENT /test.mp3 server_type audio/mpeg
ParameterTypeDescription
mountStringMountpoint name
dataStringMime type

mount.serverUrl

EVENT /test.mp3 server_url http://example.com/
ParameterTypeDescription
mountStringMountpoint name
dataStringUser-defined url

mount.slowListeners

EVENT /test.mp3 slow_listeners 0
ParameterTypeDescription
mountStringMountpoint name
dataIntegerSlow listeners number

mount.sourceIp

EVENT /test.mp3 source_ip icecast.dev
ParameterTypeDescription
mountStringMountpoint name
dataStringMounpoint stream source host or ip address

mount.streamStart

EVENT /test.mp3 stream_start 04/Aug/2015:12:00:31 +0300
ParameterTypeDescription
mountStringMountpoint name
dataStringDate, when mount started streaming

mount.title

EVENT /test.mp3 title Werkdiscs - Helena Hauff - 'Sworn To Secrecy Part II'
ParameterTypeDescription
mountStringMountpoint name
dataStringTrack name

mount.totalBytesRead

EVENT /test.mp3 total_bytes_read 1443575627
ParameterTypeDescription
mountStringMountpoint name
dataIntegerSource (incoming) traffic in bytes

mount.totalBytesSent

EVENT /test.mp3 total_bytes_sent 256000
ParameterTypeDescription
mountStringMountpoint name
dataIntegerSource (outgoing) traffic in bytes

mount.totalMBytesSent

EVENT /test.mp3 total_mbytes_sent 0
ParameterTypeDescription
mountStringMountpoint name
dataIntegerSource (outgoing) traffic in bytes

mount.ypCurrentlyPlaying

EVENT /test.mp3 yp_currently_playing Nickelback - How You Remind Me
ParameterTypeDescription
mountStringMountpoint name
dataStringTrack, that is displayed in YP

server.admin

Displays administrator's email.

EVENT global admin email@example.com
ParameterTypeDescription
dataStringAdministrator email

server.bannedIPs

EVENT global banned_IPs 0
ParameterTypeDescription
dataIntegerBanned ip addresses number

server.build

EVENT global build 20150616004931
ParameterTypeDescription
dataIntegerBuild number

server.clientConnections

EVENT global client_connections 1029675
ParameterTypeDescription
dataIntegerClient connections number

server.clients

EVENT global clients 62
ParameterTypeDescription
dataIntegerConnected clients

server.connections

EVENT global connections 1178553
ParameterTypeDescription
dataIntegerConnections number

server.fileConnections

EVENT global file_connections 3534
ParameterTypeDescription
dataIntegerFile connections number

server.host

Configuration icecast.hostname setting value. Is used for the stream directory lookups or playlist generation possibily if a Host header is not provided.

EVENT global host icecast.dev
ParameterTypeDescription
dataStringServer DNS name or IP address

server.info

Identifies the end of the big list at the beginning. When initially connected, you get a snapshot (a blast of content), and this just marks the end of it. After this then the stats are generated since the snapshot.

INFO full list end

server.listenerConnections

EVENT global listener_connections 220589
ParameterTypeDescription
dataIntegerListener connections number

server.listeners

EVENT global listeners 16
ParameterTypeDescription
dataIntegerCurrent listeners number

server.location

Configuration icecast.location setting value, is also displayed in web interface.

EVENT global location RU
ParameterTypeDescription
dataStringServer location

server.outgoingKBitrate

EVENT global outgoing_kbitrate 4411
ParameterTypeDescription
dataIntegerOutgoing bitrate (kbps)

server.serverId

Icecast server identifier. Can be overrided in config file.

EVENT global server_id Icecast 2.4.0-kh1
ParameterTypeDescription
dataStringServer identifier (icecast followed by a version number or user-defined value)

server.serverStart

EVENT global server_start 06/Jul/2015:00:19:34 +0300
ParameterTypeDescription
dataStringServer start date

server.sourceClientConnections

EVENT global source_client_connections 0
ParameterTypeDescription
dataIntegerSource client connections number

server.sourceRelayConnections

EVENT global source_relay_connections 1317
ParameterTypeDescription
dataIntegerSource relay connections number

server.sources

EVENT global sources 45
ParameterTypeDescription
dataIntegerSources number

server.sourceTotalConnections

EVENT global source_total_connections 1318
ParameterTypeDescription
dataIntegerSource total connections number

server.stats

EVENT global stats 0
ParameterTypeDescription
dataInteger?

server.statsConnections

EVENT global stats_connections 2
ParameterTypeDescription
dataInteger?

server.streamKBytesRead

EVENT global stream_kbytes_read 2414225600
ParameterTypeDescription
dataIntegerStream incoming traffic (kbytes)

server.streamKBytesSent

EVENT global stream_kbytes_sent 1102687068
ParameterTypeDescription
dataIntegerStream outgoing traffic (kbytes)

Methods

feed.connect

Establishes connection, once connected emits connect event. If you use createFeed method, it will call feed.connect automatically, so this method can be used to handle disconnects like shown below:

monitor.createFeed(function(err, feed) {
  if (err) throw err;
  
  // Handle disconnects
  feed.on('disconnect', function() {
    feed.connect();
  });
});

feed.disconnect

Closes icecast connection, once disconnected emits disconnect event.

monitor.createFeed(function(err, feed) {
  if (err) throw err;
  feed.on('connect', function() {
    
    // Disconnect with 5 seconds delay
    setTimeout(feed.disconnect, 5000);
  });
});

XmlStreamParser

Writeable stream, that allows to retrieve sources, listeners & server information from icecast xml stream. Icecast xml stream can be retrieved using monitor.createStatsXmlStream method.

Using XmlStreamParser directly can be more memory-effective when dealing with large icecast output, then using monitor.getServerInfo, monitor.getSources, monitor.getSource and monitor.getListeners methods, because those methods have to store information in memory before it is returned in callback.

// Collect sources without storing them in a memory
monitor.createStatsXmlStream('/admin/stats', function(err, xmlStream) {
  if (err) throw err;

  var xmlParser = new Monitor.XmlStreamParser();

  // Handle errors
  xmlParser.on('error', function(err) {
    console.log('error', err); 
  });
  
  // Handle server info
  xmlParser.on('server', function(server) {
    console.log('server', server);
  });
  
  // Handle sources
  xmlParser.on('source', function(source) {
    console.log('source', source);
  });

  // Handle listeners
  xmlParser.on('listener', function(listener) {
    console.log('listener', listener);
  });

  // Xml stream finished
  xmlParser.on('finish', function() {
    console.log('data is finished');
  });

  xmlStream.pipe(xmlParser);
});

Events

error

Represents error, that happened while parsing xml stream.

server

Is emitted when xml stream processing is finished. Returns following information about icecast server:

ParameterTypeDescription
adminStringAdministrator's email
bannedIPsIntegerBanned ip addresses number
buildIntegerBuild number
clientConnectionsIntegerTotal client (sources, listeners, web requests, etc) connections number
clientsIntegerCurrent clients (sources, listeners, web requests, etc) number
connectionsInteger?
fileConnectionsIntegerFile connections number
hostStringHost DNS or IP address (is defined by hostname setting in icecast config)
listenerConnectionsIntegerListeners connections number
listenersIntegerListeners number
locationStringServer location (is defined by location setting in icecast config)
outgoingKBitrateIntegerOutgoing bitrate in Kbps
serverIdStringServer identifier (is defined by server-id setting in icecast config)
serverStartStringServer start date
sourceClientConnectionsIntegerSource clients connections number
sourceRelayConnectionsIntegerSource relays connections number
sourcesIntegerSources (mountpoints) number
sourceTotalConnectionsIntegerTotal connections number
statsIntegerNumber currently connected clients using STATS HTTP method (like Monitor.Feed
statsConnectionsIntegerSTATS HTTP method total connections number
streamKBytesReadIntegerStreaming incoming traffic (KB)
streamKBytesSentIntegerStreaming outgoing traffic (KB)

source

Is emitted when source processing is finished. Returns following information for every source:

ParameterTypeDescription
mountStringMountpoint
audioCodecIdIntegerAudio codec id: 2 for mp3, 10 for aac
audioInfoStringAudio encoding information
authenticatorStringAuthentication scheme
bitrateIntegerUser-defined bitrate (Kbps)
connectedIntegerConnected time in seconds
genreStringUser-defined genre
incomingBitrateIntegerSource stream bitrate (bps)
listenerConnectionsIntegerListener connections number
listenerPeakIntegerMaximum detected number of simultaneous users
listenersIntegerCurrent listeners number
listenUrlStringAudio stream url
maxListenersIntegerListeners limit
metadataUpdatedStringLast metadata update date
mpegChannelsIntegerMpeg channels number
mpegSampleRateIntegerMpeg sample rate
outputKBitrateIntegerOutgoing bitrate for all listeners (Kbps)
publicIntegerSource advertisement: -1 - source client or relay determines if mountpoint should be advertised, 0 - disables advertisement, 1 - forces advertisement
queueSizeIntegerCan vary (typically) because lagging clients cause the size to increase until they either get kicked off or they catch up
serverDescriptionStringUser-defined description
serverNameStringUser-defined name
serverTypeStringMime type
serverUrlStringUser-defined url
slowListenersIntegerSlow listeners number
sourceIpStringSource ip address
streamStartStringDate, when stream started
titleStringTrack name
totalBytesReadIntegerIncoming traffic
totalBytesSentIntegerOutgoing traffic (Bytes)
totalMBytesSentIntegerOutgoing traffic (MBytes)
ypCurrentlyPlayingStringYP track title

listener

Is emitted when listener processing is finished. Returns following information for every listener:

ParameterTypeDescription
idIntegerIcecast internal id, can be used to kick listeners, move them between mounts, etc.
ipStringListener's ip address
userAgentStringListener's user agent
referrerStringUrl, where listener came from
lagInteger?
connectedIntegerConnected time in seconds
mountStringSource mounpoint