Skip to content

Commit

Permalink
Merge pull request #18 from foxdalas/v2
Browse files Browse the repository at this point in the history
fix exporter for version 2,3,4
  • Loading branch information
foxdalas authored Feb 10, 2022
2 parents b0a19c3 + 95796e4 commit 41d1169
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 5 deletions.
20 changes: 20 additions & 0 deletions docker-compose-2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
version: "3.9"
services:
# exporter:
# container_name: exporter
# command:
# - "--sphinx.address=sphinx"
# - "--sphinx.port=3306"
# build:
# context: .
# dockerfile: Dockerfile.compose
# ports:
# - "9247:9247"
sphinx:
container_name: sphinx
build:
context: tests/sphinx-2
ports:
- "3306:3306"


2 changes: 1 addition & 1 deletion docker-compose-3.4.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
sphinx:
container_name: sphinx
build:
context: tests/sphinx
context: tests/sphinx-3
ports:
- "3306:3306"

Expand Down
17 changes: 13 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,18 @@ func NewExporter(server string, port string, timeout time.Duration) *Exporter {
variables[variableName] = variableValue
}

version, err := semver.NewVersion(strings.Split(variables["version"], " ")[0])
if err != nil {
log.Fatal(err)
var version *semver.Version

if len(variables["version"]) == 0 {
version, err = semver.NewVersion("2.0")
if err != nil {
log.Fatal(err)
}
} else {
version, err = semver.NewVersion(strings.Split(variables["version"], " ")[0])
if err != nil {
log.Fatal(err)
}
}

return &Exporter{
Expand Down Expand Up @@ -646,7 +655,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
tid, proto, state, time, info string
)
err = threads_rows.Scan(
&tid, &proto, &state, &state, &time, &info,
&tid, &proto, &state, &time, &info,
)
if err != nil {
log.Error(err)
Expand Down
42 changes: 42 additions & 0 deletions tests/sphinx-2/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM ubuntu:xenial
MAINTAINER romeOz <[email protected]>

ENV OS_LOCALE="en_US.UTF-8" \
OS_LANGUAGE="en_US:en" \
SPHINX_LOG_DIR=/var/log/sphinxsearch \
SPHINX_CONF=/etc/sphinxsearch/sphinx.conf \
SPHINX_RUN=/run/sphinxsearch/searchd.pid \
SPHINX_DATA_DIR=/var/lib/sphinxsearch/data

RUN buildDeps='software-properties-common python-software-properties' \
&& apt-get update && apt-get install -y $buildDeps locales --no-install-recommends \
&& add-apt-repository -y ppa:builds/sphinxsearch-rel22 \
&& apt-get update \
&& apt-get install -y sudo sphinxsearch \
&& mv -f /etc/sphinxsearch/sphinx.conf /etc/sphinxsearch/origin.sphinx.conf \
&& apt-get purge -y --auto-remove $buildDeps \
&& rm -rf /var/lib/apt/lists/* \
# Forward sphinx logs to docker log collector
&& ln -sf /dev/stdout ${SPHINX_LOG_DIR}/searchd.log \
&& ln -sf /dev/stdout ${SPHINX_LOG_DIR}/query.log

# Set the locale
RUN locale-gen ${OS_LOCALE}
ENV LANG=${OS_LOCALE} \
LANGUAGE=${OS_LANGUAGE} \
LC_ALL=${OS_LOCALE}

COPY ./entrypoint.sh /sbin/entrypoint.sh
RUN chmod 755 /sbin/entrypoint.sh
RUN mkdir -p /var/lib/sphinxsearch/index/

COPY ./sphinx.conf /etc/sphinxsearch/

COPY ./docs.xml /opt/

RUN indexer --all --config "/etc/sphinxsearch/sphinx.conf"


EXPOSE 3306
VOLUME ["${SPHINX_DATA_DIR}"]
ENTRYPOINT ["/sbin/entrypoint.sh"]
22 changes: 22 additions & 0 deletions tests/sphinx-2/docs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- http://sphinxsearch.com/docs/current/xmlpipe2.html -->
<sphinx:docset>
<sphinx:schema>
<sphinx:field name="subject"/>
<sphinx:field name="content"/>
</sphinx:schema>
<sphinx:document id="1234">
<content>this is the main content <![CDATA[[and this <cdata> entry
must be handled properly by xml parser lib]]></content>
<published>1012325463</published>
<subject>note how field/attr tags can be
in <b class="red">randomized</b> order</subject>
<misc>some undeclared element</misc>
</sphinx:document>
<sphinx:document id="1235">
<subject>another subject</subject>
<content>here comes another document, and i am given to understand,
that in-document field order must not matter, sir</content>
<published>1012325467</published>
</sphinx:document>
</sphinx:docset>
130 changes: 130 additions & 0 deletions tests/sphinx-2/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/bin/bash
set -e

SPHINX_MODE=${SPHINX_MODE:-}
SPHINX_BACKUP_DIR=${SPHINX_BACKUP_DIR:-"/tmp/backup"}
SPHINX_BACKUP_FILENAME=${SPHINX_BACKUP_FILENAME:-"backup.last.tar.gz"}
SPHINX_RESTORE=${SPHINX_RESTORE:-}
SPHINX_CHECK=${SPHINX_CHECK:-}
INDEX_NAME=${INDEX_NAME:-}
SPHINX_ROTATE_BACKUP=${SPHINX_ROTATE_BACKUP:-true}

create_backup_dir() {
if [[ ! -d ${SPHINX_BACKUP_DIR}/ ]]; then
mkdir -p ${SPHINX_BACKUP_DIR}/
fi
chmod -R 0755 ${SPHINX_BACKUP_DIR}
}

create_data_dir() {
mkdir -p ${SPHINX_DATA_DIR}
chmod -R 0700 ${SPHINX_DATA_DIR}
}

rotate_backup()
{
echo "Rotate backup..."

if [[ ${SPHINX_ROTATE_BACKUP} == true ]]; then
WEEK=$(date +"%V")
MONTH=$(date +"%b")
let "INDEX = WEEK % 5" || true
if [[ ${INDEX} == 0 ]]; then
INDEX=4
fi

test -e ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz && rm ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz
mv ${SPHINX_BACKUP_DIR}/backup.tar.gz ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz
echo "Create backup file: ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz"

test -e ${SPHINX_BACKUP_DIR}/backup.${MONTH}.tar.gz && rm ${SPHINX_BACKUP_DIR}/backup.${MONTH}.tar.gz
ln ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz ${SPHINX_BACKUP_DIR}/backup.${MONTH}.tar.gz
echo "Create backup file: ${SPHINX_BACKUP_DIR}/backup.${MONTH}.tar.gz"

test -e ${SPHINX_BACKUP_DIR}/backup.last.tar.gz && rm ${SPHINX_BACKUP_DIR}/backup.last.tar.gz
ln ${SPHINX_BACKUP_DIR}/backup.${INDEX}.tar.gz ${SPHINX_BACKUP_DIR}/backup.last.tar.gz
echo "Create backup file: ${SPHINX_BACKUP_DIR}/backup.last.tar.gz"
else
mv ${SPHINX_BACKUP_DIR}/backup.tar.gz ${SPHINX_BACKUP_DIR}/backup.last.tar.gz
echo "Create backup file: ${SPHINX_BACKUP_DIR}/backup.last.tar.gz"
fi
}

import_backup()
{
echo "Import dump..."
FILE=$1
if [[ ${FILE} == default ]]; then
FILE="${SPHINX_BACKUP_DIR}/${SPHINX_BACKUP_FILENAME}"
fi
if [[ ! -f "${FILE}" ]]; then
echo "Unknown backup: ${FILE}"
exit 1
fi
create_data_dir
tar -C ${SPHINX_DATA_DIR} -xf ${FILE}
}

sed -i "s~SPHINX_DATA_DIR~${SPHINX_DATA_DIR}~g" ${SPHINX_CONF}
sed -i "s~SPHINX_LOG_DIR~${SPHINX_LOG_DIR}~g" ${SPHINX_CONF}
sed -i "s~SPHINX_RUN~${SPHINX_RUN}~g" ${SPHINX_CONF}

if [[ ${SPHINX_MODE} == indexing ]]; then
indexer --config ${SPHINX_CONF} --all
fi

if [[ ${SPHINX_MODE} == backup ]]; then
echo "Backup..."
if [[ ! -d ${SPHINX_DATA_DIR} ]]; then
echo "No such directory: ${SPHINX_DATA_DIR}"
exit 1
fi
create_backup_dir
cd ${SPHINX_DATA_DIR}
tar --ignore-failed-read -zcvf ${SPHINX_BACKUP_DIR}/backup.tar.gz *.sp* *.ram *.kill *.meta binlog.*
cd -
rotate_backup
exit 0
fi

# Restore from backup
if [[ -n ${SPHINX_RESTORE} ]]; then
import_backup ${SPHINX_RESTORE}
fi

# Check backup
if [[ -n ${SPHINX_CHECK} ]]; then

echo "Check backup..."
if [[ -z ${INDEX_NAME} ]]; then
echo "Unknown database. INDEX_NAME does not null"
exit 1;
fi

if [[ ! -d ${SPHINX_DATA_DIR} || -z $(ls -A ${SPHINX_DATA_DIR}) ]]; then
import_backup ${SPHINX_CHECK}
fi

if [[ $(indextool --config ${SPHINX_CONF} --check ${INDEX_NAME} | grep -w "check passed") ]]; then
echo "Success checking backup"
else
echo "Fail checking backup"
exit 1
fi

exit 0
fi

# allow arguments to be passed to Sphinx search
if [[ ${1:0:1} = '-' ]]; then
EXTRA_OPTS="$@"
set --
fi

# default behaviour is to launch Sphinx search
if [[ -z ${1} ]]; then
echo "Starting Sphinx search demon..."
exec $(which searchd) --config ${SPHINX_CONF} --nodetach ${EXTRA_OPTS}
else
exec "$@"
fi
39 changes: 39 additions & 0 deletions tests/sphinx-2/sphinx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
source xml
{
type = xmlpipe2
xmlpipe_fixup_utf8 = 1
xmlpipe_command = cat /opt/docs.xml
}

index test_index
{
source = xml
path = /var/lib/sphinxsearch/index/test_index

# @see http://sphinxsearch.com/docs/2.0.1/conf-blend-chars.html
blend_chars = -

# CALL SUGGEST
min_infix_len = 3

# wsparcie dla polskich znaków
# @see http://sphinxsearch.com/wiki/doku.php?id=charset_tables#polish
charset_table = 0..9, A..Z->a..z, a..z, U+0143->U+0144, U+0104->U+0105, U+0106->U+0107, U+0118->U+0119, U+0141->U+0142, U+00D3->U+00F3, U+015A->U+015B, U+0179->U+017A, U+017B->U+017C, U+0105, U+0107, U+0119, U+0142, U+00F3, U+015B, U+017A, U+017C, U+0144
}

indexer
{
mem_limit = 256M
}

searchd
{
listen = 3306:mysql41
log = /var/log/searchd.log
query_log = /var/log/query.log
query_log_format = sphinxql
pid_file = SPHINX_RUN

# binlogs
binlog_path = # disable logging
}
2 changes: 2 additions & 0 deletions tests/sphinx/Dockerfile → tests/sphinx-3/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ RUN apk add --no-cache mariadb-connector-c-dev \
postgresql-dev \
wget

http://sphinxsearch.com/downloads/sphinx-2.3.2-beta.tar.gz/thankyou.html

# set up and expose directories
RUN mkdir -pv /opt/sphinx/log /opt/sphinx/index
VOLUME /opt/sphinx/index
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 41d1169

Please sign in to comment.